多个查询与联合多个查询,哪个会更快?

发布于 2024-10-02 01:09:17 字数 868 浏览 2 评论 0原文

顾名思义,使用多个或的查询会更快,还是使用联合连接的多个查询的查询会更快?为了简单起见,我只连接了两个表,但连接中可以有更多表。

例如,

    select isbn, booktitle from book as b left join publisher as p
        on b.publisherid = p.publisherid
        where 
           booktitle like '%mysql%' or
           isbn like '%mysql%' or
           publishername like '%mysql%'  

      select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
   where 
    booktitle like '%mysql%'  
   union 
   select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
   where 
    isbn like '%mysql%' 
   union 
   select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
    where 
    publishername like '%mysql%'  

As the name suggests, a query with multiple or will be faster or a query with multiple query joined with union will be faster? I have joined only two tables to make it simple but there can be more tables in join.

for example,

    select isbn, booktitle from book as b left join publisher as p
        on b.publisherid = p.publisherid
        where 
           booktitle like '%mysql%' or
           isbn like '%mysql%' or
           publishername like '%mysql%'  

vs

      select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
   where 
    booktitle like '%mysql%'  
   union 
   select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
   where 
    isbn like '%mysql%' 
   union 
   select isbn, booktitle from book as b left join publisher as p
   on b.publisherid = p.publisherid
    where 
    publishername like '%mysql%'  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装迷糊 2024-10-09 01:09:17

我不知道你的工会会更快。您必须扫描并加入 3 次,而在第一次中,您只需扫描一次,并且对于每个加入的记录,您都可以应用条件。

I don't see how your union's would be faster. You're having to scan and join 3 times, where as in the first one, you're only scanning once, and for each joined record you can apply the criteria.

執念 2024-10-09 01:09:17

最上面的一个会更有效,因为您只查询数据一次。也就是说,如果您查询不会返回的数据,最好将这些条件放在 WHEREIN 子句中。在您提供的示例中,它看起来像这样:

SELECT isbn, booktitle
FROM book as b 
WHERE 
   booktitle LIKE '%mysql%' or
   isbn like '%mysql%' or
   b.publisherid in (SELECT publisherid FROM publisher WHERE publishername like '%mysql%')

通过过滤列(在本例中为发布者名称)上的适当索引,MySQL 将优化每个部分并更有效地组合它们。

The top one will be more efficient since you're only querying the data once. That said, if you're querying against data you will not be returning, it's even better to put those criteria in an IN clause in the WHERE. In your provided example, it would look like this:

SELECT isbn, booktitle
FROM book as b 
WHERE 
   booktitle LIKE '%mysql%' or
   isbn like '%mysql%' or
   b.publisherid in (SELECT publisherid FROM publisher WHERE publishername like '%mysql%')

With proper indexes on the filtering columns (publishername, in this case), MySQL will optimize each piece and combine them much more efficiently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文