简体   繁体   English

如何优化sql join查询

[英]how to optimize sql join query

I want to optimize sql query 我想优化SQL查询

SET SQL_BIG_SELECTS=1;
SELECT
    `Surveys`.`fname`
    , `Surveys`.`lname`
    , `Surveys`.`smobile`
    , `Surveys`.`semail`    
    , `Surveys`.`country`
    , `Surveys`.`city`    
    , `Surveys`.`sdob`
    , `Brand`.`brandname`
    , `Product`.`productname`        
    , `Surveys`.`outletcode`
    , `Surveys`.`outletname`
    , `Surveys`.`coupon_no`
    , `Users`.`username`
    , DATE_ADD(`Surveys`.datetime, INTERVAL 8 HOUR) as datetime
    , `Surveys`.`duration`    
    , userentry.couponcode as wcouponcode
    , userentry.couponcodecountry
    , userentry.prizename
    , DATE_ADD(userentry.datetime, INTERVAL 8 HOUR) as wdatetime    
FROM
    `Surveys`
    INNER JOIN `Brand` 
        ON (`Surveys`.`brandid` = `Brand`.`brandid`)
    INNER JOIN `Product` 
        ON (`Surveys`.`productid` = `Product`.`productid`) AND (`Surveys`.`brandid` = `Product`.`brandid`)    
    INNER JOIN `Users` 
        ON (`Surveys`.`userid` = `Users`.`userid`)
    INNER JOIN `userentry` 
        ON (`userentry`.`mobile` = `Surveys`.`smobile`)

here if am not writing SET SQL_BIG_SELECTS=1; 这里如果没有写SET SQL_BIG_SELECTS = 1; it doesn't work 它不起作用

even with SQL_BIG_SELECTS its expire(sql timeout), 即使SQL_BIG_SELECTS到期(sql超时),

so how to optimize this query 所以如何优化这个查询

Please help me 请帮我

There are always 2 things to consider when optimising queries: 优化查询时总有两件事需要考虑:

  • What indexes can be used (you may need to create indexes) 可以使用哪些索引(您可能需要创建索引)

  • How the query is written (you may need to change the query to allow the query optimiser to be able to find appropriate indexes, and to not re-read data redundantly) 如何编写查询(您可能需要更改查询以允许查询优化器能够找到适当的索引,并且不会冗余地重新读取数据)

The keys are: 关键是:

1.You shouldn't need the subqueries - just do the direct joins and aggregate 1.您不应该需要子查询 - 只需进行直接连接和聚合

2.You should be able to use INNER JOINs, which are typically more efficient than OUTER JOINs 2.您应该能够使用INNER JOIN,这通常比OUTER JOIN更有效

您必须索引在select语句中使用的列(brandId,productid,userid,mobile)

应该为连接中包含的两个表之间的公共列建立索引。

From what the others noted about ensuring an index on the columns you are joining on, I only have one other suggestion. 从其他人关于确保您加入的列的索引的注意事项来看,我只有另外一个建议。 SOMETIMES, the MySQL query optimizer will try to use one of the other tables (such as product, brand, user) as the driving table based on lower counts from those tables or whatever other stats it may have available. 有时,MySQL查询优化器将尝试使用其他表之一(例如产品,品牌,用户)作为驱动表,基于这些表中的较低计数或其可能具有的任何其他统计数据。

Since your query looks like all the other tables are more "lookup" reference only, and your Surveys table is the critical root table, just change 由于您的查询看起来像所有其他表只是更多“查找”参考,并且您的Surveys表是关键的根表,只需更改

SELECT (... rest of query) SELECT(...查询的其余部分)

to

SELECT STRAIGHT_JOIN (... rest of query) SELECT STRAIGHT_JOIN(...查询的其余部分)

It tells the optimizer to do it in the order you've specified. 它告诉优化器按照您指定的顺序执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM