首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更有效地编写Mysql查询

更有效地编写Mysql查询
EN

Stack Overflow用户
提问于 2016-01-04 05:47:08
回答 4查看 63关注 0票数 1

如何使下面的Mysql查询更高效?

代码语言:javascript
复制
SELECT
   DISTINCT crm_task_id,
   table_header_id  
FROM
   table_details  
WHERE
   table_header_id  IN (
      SELECT
         table_header_id  
      FROM
         table_header  
      WHERE
         crm_campaign_id =196  
         AND crm_campaign_post_code_id  IN (
            SELECT
               crm_campaign_post_code_id  
            FROM
               crm_campaign_post_code  
            WHERE
               is_display_operator  IN (
                  1, 0 
               )  
         )  
         AND g_user_id  IN (
            SELECT
               g_user_id  
            FROM
               crm_user  
            WHERE
               is_active =1  
         )  
         AND DATE_FORMAT( created,  '%Y-%m-%d' )   BETWEEN  '2015-12-01'  AND  '2016-01-04'  
      )  
      AND crm_task_id NOT   IN (
         SELECT
            crm_task_id  
         FROM
            table_details  
         WHERE
            table_header_id  IN (
               SELECT
                  table_header_id  
               FROM
                  table_header  
               WHERE
                  crm_campaign_id =196  
                  AND crm_post_code_categ_id !=1000  
            )  
         )

table_header列:

代码语言:javascript
复制
| table_header_id           | bigint(20)  
| created                   | datetime     
| updated                   | datetime    
| createdby                 | bigint(20)   
| updatedby                 | bigint(20)   
| is_active                 | char(1)     
| crm_user_session_id       | bigint(20)   
| crm_campaign_id           | bigint(20)   
| crm_post_code_categ_id    | bigint(20)  
| value                     | varchar(128) 
| crm_campaign_post_code_id | bigint(20)   
| crm_filter_id             | bigint(20)  
| g_user_id                 | bigint(20)  
| session_time              | int(100) 

table_details列:

代码语言:javascript
复制
| table_details_id | bigint(11)  
| table_header_id         | bigint(11) 
| created                   | datetime    
| updated                   | datetime  
| createdby                 | bigint(11)
| updatedby                 | bigint(11) 
| is_active                 | smallint(5)
| crm_contact_id            | varchar(60)
| crm_task_id               | bigint(11)

以上查询需要超过2秒才能返回结果,请任何人重写此查询以返回结果更快!

EN

回答 4

Stack Overflow用户

发布于 2016-01-04 05:59:08

试试这个。

代码语言:javascript
复制
SELECT
   DISTINCT td.crm_task_id,
   td.table_header_id      
FROM
   table_details td      
join
   table_header th 
      on th.table_header_id = td.table_header_id 
      and th.crm_campaign_id =196 
      and th.crm_post_code_categ_id !=1000      
join
   crm_campaign_post_code ccpc 
      on ccpc.crm_compaign_post_code_id =  th.crm_campaign_post_code_id 
      and ccpc.is_display_operator IN (
         1,
      0 ) 
      and DATE_FORMAT( created,
      '%Y-%m-%d' ) BETWEEN  '2015-12-01' AND  '2016-01-04'      
   join
      crm_user cr 
         on cr.g_user_id = ccpc.g_user_id 
         and cr.is_active = 1
票数 1
EN

Stack Overflow用户

发布于 2016-01-04 06:13:44

尝尝这个

代码语言:javascript
复制
 SELECT DISTINCT crm_task_id, td.table_header_id
    FROM table_details td
    WHERE table_header_id 
    JOIN table_header th
    ON td.table_header_id = th.table_header_id
    AND th.crm_campaign_id =196
    JOIN crm_campaign_post_code c
    ON c.crm_campaign_post_code_id = th.crm_campaign_post_code_id
    AND is_display_operator IN ( 1, 0 ) 
    JOIN crm_user u
    ON u.g_user_id = c.g_user_id
    AND is_active =1
    AND DATE_FORMAT( created,  '%Y-%m-%d' ) 
    BETWEEN  '2015-12-01'
    AND  '2016-01-04'
    WHERE u.crm_task_id  NOT 
    IN (
    SELECT IFNULL(crm_task_id,0)
    FROM table_details
    WHERE table_header_id
    IN (
    SELECT table_header_id
    FROM table_header
    WHERE crm_campaign_id =196
    AND crm_post_code_categ_id !=1000
   ))
票数 1
EN

Stack Overflow用户

发布于 2016-01-04 09:10:00

试试这个:

代码语言:javascript
复制
    SELECT DISTINCT crm_task_id, table_header_id
FROM table_details
WHERE table_header_id
IN (
    select table_header_id From 
    (
    SELECT table_header_id
    FROM table_header
    WHERE crm_campaign_id =196
    AND crm_campaign_post_code_id
    IN (

        select crm_campaign_post_code_id from (
        SELECT crm_campaign_post_code_id
        FROM crm_campaign_post_code
        WHERE is_display_operator
        IN ( 1, 0 )
        ) temp2
    )
    AND g_user_id
    IN (
        select g_user_id from (
        SELECT g_user_id
        FROM crm_user
        WHERE is_active =1
        ) temp3
    )
    AND DATE_FORMAT( created,  '%Y-%m-%d' ) BETWEEN  '2015-12-01' AND  '2016-01-04'
    ) as temp_1
)
AND crm_task_id NOT IN (
    SELECT crm_task_id FROM table_details WHERE table_header_id
    IN (
        select table_header_id From (
        SELECT table_header_id
        FROM table_header
        WHERE crm_campaign_id =196
        AND crm_post_code_categ_id !=1000
        ) temp4
    )
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34585353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档