我有一个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)
问题是由活动管理员和一些动态的自定义索引范围引起的。要修复它,您可以禁用或调整这些自定义范围。
例如:
=> 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