简体   繁体   English

比连接更好的解决方案

[英]Better solution than joins

I have a mySQL query with a join on 11 tables with approximately 50000 rows in each table, all of them linked by a serial number common in all tables 我有一个带有11个表的连接的mySQL查询,每个表中有大约50000行,所有这些表都通过所有表中常见的序列号链接
Although i only want one row per serialnumber, but mySQL takes ages to run the query as it has to join so many tables with so many rows 虽然我只需要每个序列号一行,但mySQL需要很长时间才能运行查询,因为它必须连接如此多的表以及如此多的行
i believe it has to create around 50000^11 (11 in power) 我相信它必须创造大约50000 ^ 11(11的权力)

Example: (the sample query may not be syntactically correct but i hope you get the idea) 示例:(示例查询可能在语法上不正确但我希望您明白这一点)

select distinct serial, one.data1,two.data2,three.data3 from list 
left join (select * from table1)  as one on list.serial=one.serial 
left join (select * from table2)  as two on list.serial=two.serial 
left join (select * from table3)  as three on list.serial=three.serial 

are joins better or sub queries? 是加入更好还是子查询?
is there a better way to build the sql query which takes less time? 是否有更好的方法来构建sql查询,花费更少的时间?
if yes, how? 如果有,怎么样?

One solution is to create and maintain a separate index table that consists of the datapoints that you need (serial, one.data1, two.data2, three.data3 for example). 一种解决方案是创建和维护一个单独的索引表,该表包含您需要的数据点(例如,serial,one.data1,two.data2,three.data3)。 Reindexing (the query and insertion of records) this table will result in overhead, but all subsequent queries can save a lot of overhead. 重新索引(查询和插入记录)此表将导致开销,但所有后续查询都可以节省大量开销。

Another solution that I have found useful is to perform programatic joins. 我发现另一个有用的解决方案是执行程序连接。 When multi-table joins with simple criteria require too much memory, there can be a massive performance degradation. 当具有简单标准的多表连接需要太多内存时,可能会出现大量性能下降。 You should try benchmarking manually joining datasets built from easy queries using whatever native programming language you are working in (PHP for example). 您应该尝试使用您正在使用的任何本机编程语言(例如PHP)手动加入从简单查询构建的数据集的基准测试。 Sometimes you might get an edge using this technique 有时你可能会使用这种技术获得优势

  • contains tables foreign keys? 包含表外键?

  • don't use select *, but join table 不要使用select *,而是连接表

maybe: 也许:

SELECT serial, table1.data1,table2.data2,table3.data3 FROM list
LEFT JOIN table1 USING serial
LEFT JOIN table2 USING serial
LEFT JOIN table3 USING serial
GROUP BY list.serial
  • create keys 创建密钥

maybe process queries separately and link data in PHP 也许可以单独处理查询并在PHP中链接数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM