在mysql中为join some表创建索引

jgwigjjp  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(464)

我对mysql数据库的sql查询如下:

SELECT
    count(*) AS count,
    sum(total) AS total
FROM
    vtiger_salesorder
INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_salesorder.salesorderid
INNER JOIN vtiger_sobillads ON vtiger_salesorder.salesorderid = vtiger_sobillads.sobilladdressid
INNER JOIN vtiger_soshipads ON vtiger_salesorder.salesorderid = vtiger_soshipads.soshipaddressid
INNER JOIN vtiger_salesordercf ON vtiger_salesordercf.salesorderid = vtiger_salesorder.salesorderid
INNER JOIN vtiger_account ON vtiger_account.accountid = vtiger_salesorder.accountid
LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_salesordercf.cf_456
LEFT JOIN vtiger_contactdetails ON vtiger_salesorder.contactid = vtiger_contactdetails.contactid
LEFT JOIN vtiger_soshop ON vtiger_soshop.salesorderid = vtiger_salesorder.salesorderid
WHERE
    vtiger_crmentity.deleted = 0
ORDER BY
    vtiger_crmentity.modifiedtime DESC;

请单击此处显示以上sql解释
你能帮我创建一些索引来提高这个查询的性能吗。谢谢

mnemlml8

mnemlml81#

这些索引可能有助于:

crmentity:   (deleted, modifiedtime)  -- in this order
salesorder:  (salesorderid)

但是。。。
如果所有的 JOINs 是1:1,你只需要一张table total 在里面。
如果一些 JOINs 是1:很多 , then 计数(*) and/or sum(total)`将得到膨胀值!

gcmastyq

gcmastyq2#

如果以下三个复合索引带来任何改进,您可以尝试一下。

CREATE INDEX idxCrmidDeleted
             ON vtiger_crmentity
                (crmid,
                 deleted);

CREATE INDEX idxSalesorderidAccountidContactid
             ON vtiger_salesorder
                (salesorderid,
                 accountid,
                 contactid);

CREATE INDEX idxSalesorderidCf_456
             ON vtiger_salesordercf
                (salesorderid,
                 cf_456);

检查执行计划。
柱子在哪里 total 从哪里来?如果是上面的索引所在的表中的任何一个,也可以尝试上面这样的索引,但另外还有 total 作为最后一列。例如

CREATE INDEX idxSalesorderidAccountidContactid
             ON vtiger_salesorder
                (salesorderid,
                 accountid,
                 contactid,
                 total);

如果 totalvtiger_salesorder . 那么就有可能只有索引被单独查询。

相关问题