首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNION或UNION所有MYSQL优化

UNION或UNION所有MYSQL优化
EN

Stack Overflow用户
提问于 2017-07-11 18:25:58
回答 2查看 45关注 0票数 0

我一直试图优化下面的MySQL查询,但没有结果。它目前需要3秒才能执行:

代码语言:javascript
复制
select count( DISTINCT v.id) from 
( 
    select sub_jobs.id from sub_jobs, main_jobs where sub_jobs.title LIKE '%abc%' and main_jobs.id=sub_jobs.parent_id 

    UNION ALL 

    select sub_jobs.id from sub_jobs, main_jobs where job_title LIKE '%abc%' and main_jobs.id=sub_jobs.parent_id 

    UNION ALL 

    select sub_jobs.id from sub_jobs, main_jobs, companies where companies.company_name LIKE '%abc%' and main_jobs.company_id=companies.id and main_jobs.id=sub_jobs.parent_id 
) 
as v

我想做什么:

  • 在3个表中搜索三个列中的一个关键字,并返回不同的计数

基本表结构:

1. sub_jobs

  • id
  • 标题我想搜索这个专栏
  • parent_id

2. main_jobs

  • id
  • job_title我想搜索这个专栏
  • company_id某些行没有该列的条目

3.公司

  • id
  • company_name我想搜索这个专栏

解释

代码语言:javascript
复制
------column titles------

id
select_type:
table
type
possible_keys
key
key_len
ref
rows
Extra

--- row 1 ----

id:  1
select_type: PRIMARY
table: <derived2>
ALL
NULL
NULL
NULL
NULL
102642
NULL


---- row 2 ----

id: 2
select_type: DERIVED
table: main_jobs
index
PRIMARY,id_index,id_industry_featured_index
id_index
4
NULL
34214
Using index


---- row 3 ----

id: 2
select_type: DERIVED
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
myjobmag_myjobdb.main_jobs.id
1
Using where


---- row 4 ----

id: 3
select_type: UNION
table: main_jobs
ALL
PRIMARY,id_index,id_industry_featured_index
NULL
NULL
NULL
34214
Using where

---- row 5 ----

id: 3
select_type: UNION
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
main_jobs.id
1
Using index

---- row 6 ----


id: 4
select_type: UNION
table: main_jobs
ALL
PRIMARY,id_index,id_industry_featured_index
NULL
NULL
NULL
34214
NULL

---- row 7 ----

id: 4
select_type: UNION
table: companies
eq_ref
PRIMARY
PRIMARY
4
main_jobs.company_id
1
Using where


---- row 8 ----

id: 4
select_type: UNION
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
main_jobs.id
1
Using index


---- row 9 ----

id: NULL
select_type: UNION RESULT
table: <union2,3,4>
ALL
NULL
NULL
NULL
NULL
NULL
Using temporary

我试过使用UNION而没有显著的收益。

如何减少此查询的运行时。

EN

回答 2

Stack Overflow用户

发布于 2017-07-11 18:33:20

试图减少一个条件。请试着让我知道

代码语言:javascript
复制
select count( DISTINCT v.id) from 
( 
    select sub_jobs.id 
    from sub_jobs inner join main_jobs
    on main_jobs.id=sub_jobs.parent_id  
    where (sub_jobs.title LIKE '%abc%' OR job_title LIKE '%abc%') 

    UNION ALL 

    select sub_jobs.id 
    from sub_jobs, main_jobs, companies
    where companies.company_name LIKE '%abc%' and
          main_jobs.company_id=companies.id and 
          main_jobs.id=sub_jobs.parent_id 
) 
as v

问题更新后的编辑

可以合并以下两个查询:

代码语言:javascript
复制
select count( DISTINCT v.id) from 
( 
    select sub_jobs.id 
    from sub_jobs 
         inner join main_jobs
          on main_jobs.id=sub_jobs.parent_id 
         inner join companies 
          on main_jobs.company_id=companies.id
    where companies.company_name LIKE '%abc%'
          OR
          sub_jobs.title LIKE '%abc%' 
          OR 
          job_title LIKE '%abc%'       
) 
as v

请让我知道它是否工作或抛出错误。

票数 0
EN

Stack Overflow用户

发布于 2017-07-11 18:37:38

您可以尝试使用单个quert或使用union代替3查询。

但你应该看看解释计划

代码语言:javascript
复制
    select count(dictinnct sub_jobs.id )
    from sub_jobs
    INNER JOIN main_jobs on main_jobs.id=sub_jobs.parent_id 
    INNER JOIN companies on main_jobs.company_id=companies.id
    where companies.company_name LIKE '%abc%' 
    OR main_jobs.job_title LIKE '%abc%'
    OR sub_jobs.title LIKE '%abc%' 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45042042

复制
相关文章

相似问题

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