2014-01-23 83 views
0

我有一個Rails應用程序,在服務器啓動時運行很多可能不必要的數據查詢。Ruby On Rails在服務器啓動後運行一些數據庫查詢?

我想知道如何防止這種情況。有沒有辦法來檢測查詢運行的是什麼?

我的應用使用活動管理員。提前致謝。

$ rails server 
=> Booting Thin 
=> Rails 3.2.16 application starting in development on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
Connecting to database specified by database.yml 
    Event Load (112.3ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    EXPLAIN (97.8ms) EXPLAIN SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
EXPLAIN for: SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
+----+-------------+--------+------+----------------------------+----------------------------+---------+-------+------+-----------------------------+ 
| id | select_type | table | type | possible_keys    | key      | key_len | ref | rows | Extra      | 
+----+-------------+--------+------+----------------------------+----------------------------+---------+-------+------+-----------------------------+ 
| 1 | SIMPLE  | events | ref | index_events_on_is_enabled | index_events_on_is_enabled | 2  | const | 2 | Using where; Using filesort | 
+----+-------------+--------+------+----------------------------+----------------------------+---------+-------+------+-----------------------------+ 
1 row in set (0.10 sec) 

    Team Load (99.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.0ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (101.0ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Language Load (98.1ms) SELECT `languages`.* FROM `languages` 
    Event Load (105.8ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.8ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.7ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (105.8ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.3ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. 
    Event Load (199.4ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 
    Team Load (100.2ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (104.8ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (99.0ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.4ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (104.8ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (99.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.2ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (98.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Language Load (97.9ms) SELECT `languages`.* FROM `languages` 
    Event Load (106.1ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (97.6ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (102.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (126.3ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (104.9ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.3ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.4ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (104.7ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (99.0ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (97.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (100.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (99.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    Event Load (104.7ms) SELECT `events`.* FROM `events` WHERE `events`.`is_enabled` = 1 ORDER BY start_date_time 
    Team Load (98.9ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 365 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (99.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 382 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (99.7ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 2503 ORDER BY abbreviation ASC LIMIT 1 
    Team Load (99.1ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`espn_id` = 160 ORDER BY abbreviation ASC LIMIT 1 
    LeaderBoardRange Load (98.8ms) SELECT `leader_board_ranges`.* FROM `leader_board_ranges` WHERE `leader_board_ranges`.`id` = 1 LIMIT 1 
    LeaderBoardRange Load (99.1ms) SELECT `leader_board_ranges`.* FROM `leader_board_ranges` WHERE `leader_board_ranges`.`id` = 1 LIMIT 1 
    EventPollRelationship Load (103.9ms) SELECT DISTINCT `event_poll_relationships`.* FROM `event_poll_relationships` INNER JOIN `votes` ON `votes`.`event_poll_relationship_id` = `event_poll_relationships`.`id` WHERE (votes.created_at > '2014-01-24 13:38:50') 
Thin web server (v1.6.1 codename Death Proof) 
Maximum connections set to 1024 
Listening on 0.0.0.0:3000, CTRL+C to stop 
+0

你可以添加查詢的一些例子嗎? –

+0

你將不得不提供更多信息,否則沒有人能夠提供幫助。 – seanlinsley

回答

0

該問題是由活動管理員和一些動態的自定義索引作用域引起的。要修復它,您可以禁用或調整這些自定義範圍。

如:

=> Booting Thin 
=> Rails 3.2.16 application starting in development on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
Connecting to database specified by database.yml 
Thin web server (v1.6.1 codename Death Proof) 
Maximum connections set to 1024 
Listening on 0.0.0.0:3000, CTRL+C to stop 
相關問題