【发布时间】:2013-07-11 19:39:54
【问题描述】:
我想听听您对以下查询的看法:
select a.expense_code, a.expense_date, a.expense_supplier_code, b.supplier_name, a.expense_discount, a.expense_payment_method, a.expense_payment_transfer_to, a.expense_advance, a.expense_status,
sum(c.expense_item_buy_price * c.expense_item_quantity) , d.account_name, a.expense_counter, a.expense_type, a.expense_saving_type, a.expense_payment_transfer_from
from expense_data a, supplier_data b, expense_item c, tree_data d
where a.expense_supplier_code = b.supplier_code and a.expense_payment_transfer_to= d.account_code
and a.expense_counter = c.expense_counter
and a.expense_date between '2013-01-01' and '2014-01-01' and a.expense_status = 0 or a.expense_status = 2 group by (a.expense_counter);
即使在expense_data 表中有四个索引,此查询也花费了很多时间:
1- Expense_code.
2- expense_user_id
3- expense_supplier_code
4- expense_payment_transfer_from
我不知道为什么要花这么多时间是因为两个太多的加入还是因为太多的indeces。你能建议吗?
【问题讨论】:
-
你看过运算符优先级吗?并且具有比 or 更高的优先级。最后,你写
a.expense_status = 0 or a.expense_status = 2。我认为这应该放在括号中,或者您应该将其更改为a.expense_status in (0, 2),除非您实际上的意思是应该返回任何状态为 2 的行,而不管其他条件如何。 -
运行'EXPLAIN EXTENDED'+ YourQuery Code;并将结果发布到您上面的问题中,否则我们不知道表的大小、类型、索引用于执行查询的方式等。
-
别忘了使用执行计划。