需要帮助优化查询
我有两个表 - 第一个表中的 incomingtours(id,name)
和 incoming_tours_cities(id_parrent, id_city)
id
是唯一的,并且对于每个表第一个表中的唯一行有第二个表中的 id_city
- s 列表(即第二个表中的 id_parrent
等于第一个表中的 id
)
例如
incoming_tours
|--id--|------name-----|
|---1--|---first_tour--|
|---2--|--second_tour--|
|---3--|--thirth_tour--|
|---4--|--hourth_tour--|
incoming_tours_cities
|-id_parrent-|-id_city-|
|------1-----|---4-----|
|------1-----|---5-----|
|------1-----|---27----|
|------1-----|---74----|
|------2-----|---1-----|
|------2-----|---5-----|
........................
这意味着 first_tour
有城市列表 - ("4","5","27" ,"74")
AND second_tour
有城市列表 - ("1","5")
让我们假设我有两个值 - 4
和 74
:
现在,我需要从第一个表中获取所有行,其中我的两个值都在城市列表中。即它必须只返回 first_tour (因为 4 和 74 在它的城市列表中)
所以,我编写了以下查询
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
并且效果很好。
但我动态生成查询,当连接数量很大(大约 15)时,查询速度会变慢。
即当我尝试
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
.........................................................
JOIN `incoming_tours_cities` tc15 ON tc15.id_parrent = t.id
AND tc15.id_city = 'some_value'
在 45s
内运行查询时(尽管我在表中设置了索引)
我能做什么来优化它?
非常感谢
I have two tables - incoming tours(id,name)
and incoming_tours_cities(id_parrent, id_city)
id
in first table is unique, and for each unique row from first table there is the list of id_city
- s in second table(i.e. id_parrent
in second table is equal to id
from first table)
For example
incoming_tours
|--id--|------name-----|
|---1--|---first_tour--|
|---2--|--second_tour--|
|---3--|--thirth_tour--|
|---4--|--hourth_tour--|
incoming_tours_cities
|-id_parrent-|-id_city-|
|------1-----|---4-----|
|------1-----|---5-----|
|------1-----|---27----|
|------1-----|---74----|
|------2-----|---1-----|
|------2-----|---5-----|
........................
That means that first_tour
has list of cities - ("4","5","27","74")
AND second_tour
has list of cities - ("1","5")
Let's assume i have two values - 4
and 74
:
Now, i need to get all rows from first table, where my both values are in the list of cities. i.e it must return only the first_tour (because 4 and 74 are in it's list of cities)
So, i wrote the following query
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
And that works fine.
But i generate the query dynamically, and when the count of joins is big (about 15) the query slowing down.
i.e. when i try to run
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
.........................................................
JOIN `incoming_tours_cities` tc15 ON tc15.id_parrent = t.id
AND tc15.id_city = 'some_value'
the query run's in 45s
(despite on i set indexes in the tables)
What can i do, to optimaze it?
Thanks much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
但您需要更改城市总数。
But you need to change number of total cities count.
您必须将
c=2
更改为您正在搜索的id_city
计数,但由于您是动态生成查询,因此这应该不是问题。You will have to change
c=2
to whatever the count ofid_city
you are searching is, but since you generate the query dynamically, that shouldn't be a problem.我很确定这可行,但不太确定它是否是最佳的。
I'm pretty sure this works, but a lot less sure that it is optimal.
只是一个提示。
如果您在
WHERE
子句中使用IN
运算符,则可以希望运算符AND
的短路可以删除不必要的JOIN
在不遵守约束的游览执行期间。Just an hint.
If you use the
IN
operator in aWHERE
clause, you can hope that the short-circuit of operatorAND
may remove unnecessaryJOIN
s during the execution for the tours that do not respect the constraint.执行该查询的方式似乎很奇怪,这里
我认为可以做到这一点,但未经测试...
编辑:向子查询添加表别名
Seems like an odd way to do that query, here
I think that does it, but not tested...
EDIT: Added table alias to sub-query
我使用 CTE 编写了此查询,它在查询中包含测试数据。您需要修改它,以便它查询真实的表。不确定它在大型数据集上的表现如何......
I've written this query using CTE's and it includes the test data in the query. You'll need to modify it so that it queries the real tables instead. Not sure how it performs on a large dataset...