如何有效地索引这些查询

时间:2011-06-28 23:37:54

标签: ruby-on-rails database ruby-on-rails-3 indexing

我的应用程序中有一个区域正在执行大量复杂查询,每页从1000s表中返回10条记录。以下是一些样本。

SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.company_id = 21) AND (employee_id IS NULL) AND (status != 'Complete') AND (status != 'Cancelled') ORDER BY scheduled desc LIMIT 0, 30;

SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.company_id = 21) AND (employee_id = 0) AND (status != 'Complete') AND (status != 'Cancelled') AND (scheduled IS NOT NULL) AND (scheduled > '2011-03-28' AND scheduled < '2011-09-28');

SELECT `tasks`.* FROM `tasks` WHERE (`tasks`.employee_id = 27) AND (status != 'Complete') AND (status != 'Cancelled') ORDER BY scheduled desc LIMIT 0, 30
始终存在

employee_idstatus; company_id几乎总是存在,scheduled只是默认排序,用户可以更改。

我尝试了这些索引:

# ignored for some reason - did not seem to build successfully in the db
add_index :tasks, [:company_id, :employee_id, :status, :scheduled]

# db favorite, but often results in FILESORT
add_index :tasks, [:employee_id, :status, :scheduled]

处理问题并减少我在这些查询中获得的FILESORTS数量的最佳方法是什么?

如果我能提供更详细的信息,请告诉我。

0 个答案:

没有答案