zoukankan      html  css  js  c++  java
  • mysql高效获取两张表共同字段的交集数据

    问题:

    例如下面两站表A,B.A表和B表分别有5-10w数据。
    A表结构如下:
    id bid name title publisher extra
    B表结构如下
    id bid name title publisher
    A出版社也为很多人出版了书籍,B出版社也为很多人出版了书籍,有sql语句找出这两个出版社为那些人 共同出版书籍,用innerjoin太慢,有没有什么更好的办法?

    解答一:

    由于不知道你表的索引情况,至于用join还是in和exists不太好说,理论上讲,exists最快、in次之、join最慢,但是这个不是绝对的,还需要看你的索引建立。建议你把表索引贴出来,或者分别用explain看看这三种方式MySQL的执行计划。然后再做相应的调优

    解答二:

    1. SELECT id, bid, name, title, publisher FROM A where publisher in (select publisher from B group by B.publisher)

    或者

    1. SELECT id, bid, name, title, publisher FROM A where exists (select publisher from B where B.publisher = A.publisher group by B.publisher)

    用EXISTS应该效率更高,另外对查询条件字段publisher建立索引。

  • 相关阅读:
    defer与async的区别
    Promise 的含义
    SCSS 与 Sass 异同
    CSS总结2
    CSS总结1
    jQuery-插件,优化
    jQuery-表格以及表单
    jQuery-事件以及动画
    jQuery-ajax
    jQuery-DOM操作
  • 原文地址:https://www.cnblogs.com/Alight/p/3873437.html
Copyright © 2011-2022 走看看