多重Join或子查询查询优化
我有这个查询,对于给定的情况,它会在 1 或 2 秒内执行:
Select Count(*) as qtty
From event e
Join org o On o.orgID = e.orgID
Join venue v On v.venueID = e.venueID
Where Match( e.name, e.description ) Against ( $keywords )
And e.site_id = $site_id
And e.display <> 0</code>
它计算行数以构建分页。当我引入按事件类型过滤(类型与事件多对多相关)时,查询开始花费不少于 45 秒的时间:
And Exists (
Select ete.id
From event_type_to_event ete
Where ete.event_id = e.eventID
And ete.event_type_id = $category )</code>
我还尝试了使用 event_type_to_event 进行连接,但速度更慢。
有什么建议吗?
注:已解决。使用索引,查询执行时间降至不到一秒。
I have this query, which executes in 1 or 2 seconds for a given case:
Select Count(*) as qtty
From event e
Join org o On o.orgID = e.orgID
Join venue v On v.venueID = e.venueID
Where Match( e.name, e.description ) Against ( $keywords )
And e.site_id = $site_id
And e.display <> 0</code>
It counts the rows to build the pagination. When I introduced filtering by event type (types are related many to many to events) the query started taking no less than 45 seconds:
And Exists (
Select ete.id
From event_type_to_event ete
Where ete.event_id = e.eventID
And ete.event_type_id = $category )</code>
I also tried a Join with event_type_to_event but it was even slower.
Any suggestions?
NOTE: Solved. Using indices, the query execution time went down to less than a second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您需要在表 event_type_to_event 中的 event_type_id 列上添加索引,但如果那里已经有索引,请尝试以下操作:
如果 Event_Id 是表 event_type_to_event 的 PK,您也可以尝试 连接而不是使用 Exists,
I suspect you need to add an index on the column event_type_id in table event_type_to_event, but if there is already an index there, then try the following:
If Event_Id is the PK of table event_type_to_event you can also try a join instead of using Exists,