简体   繁体   English

如何通过多个连接加速从 MySQL 查询加载数据

[英]How to speed up loading data from MySQL query with multiple joins

When I run this query it only displays loading that is not finished.当我运行此查询时,它只显示未完成的加载。 So the time to run the query has run out without displaying any data.因此,运行查询的时间已经用完,而没有显示任何数据。 In fact, making computer operations become very slow.事实上,这使得计算机操作变得非常缓慢。

Here is the problem.这是问题所在。 I am selecting and doing multiple joins to get the correct items... This query takes so long time.我正在选择并执行多个连接以获取正确的项目...此查询需要很长时间。 Anyone know how I can speed this up?有谁知道我如何加快速度? Here is the query.这是查询。

SELECT a.*, 
LEFT(a.tgl_inovasi, 10) AS tgl_ino, b.`nm_unit`, c.`nm_inovasijns`, 
d.`nm_regulasi`, e.`nm_sediasdm`, f.`nm_anggaran`, g.`nm_it`, h.`nm_bimtek`, 
i.`nm_renstra`, j.`nm_jejaring`, k.`nm_replikasi`, l.`nm_pedoman`, 
m.`nm_pengelola`, n.`nm_informasi`, o.`nm_penyelesaian`, p.`nm_partisipasi`, 
q.`nm_kemudahaninf`, r.`nm_kemudahanpro`, s.`nm_online`, t.`nm_kecepatan`, 
u.`nm_kemanfaatan`, v.`nm_kepuasan`, w.`nm_user`, x.nm_inovasikat 
FROM inovasi a, unit b, inovasijns c, regulasi d, sediasdm e, anggaran f, 
it g, bimtek h, renstra i, jejaring j, replikasi k, pedoman l, pengelola m, 
informasi n, penyelesaian o, partisipasi p, kemudahaninf q, kemudahanpro r, 
online s, kecepatan t, kemanfaatan u, kepuasan v, `user` w, inovasikat x 
WHERE x.id_inovasikat = a.id_inovasikat AND w.`id_user` = a.`id_user` AND 
v.`id_kepuasan` = a.`id_kepuasan` AND u.`id_kemanfaatan` = a.`id_kemanfaatan` AND 
t.`id_kecepatan` = a.`id_kecepatan` AND s.`id_online` = a.`id_online` AND 
r.`id_kemudahanpro` = a.`id_kemudahanpro` AND q.`id_kemudahaninf` = 
a.`id_kemudahaninf` AND p.`id_partisipasi` = a.`id_partisipasi` AND 
o.`id_penyelesaian` = a.`id_penyelesaian` AND n.`id_informasi` = a.`id_informasi` 
AND m.`id_pengelola` = a.`id_pengelola` AND l.`id_pedoman` = a.`id_pedoman` AND 
k.`id_replikasi` = a.`id_replikasi` AND j.`id_jejaring` = a.`id_jejaring` AND 
i.`id_renstra` = a.`id_renstra` AND h.`id_bimtek` = a.`id_bimtek` AND g.`id_it` = 
a.`id_it` AND f.`id_anggaran` = a.`id_anggaran` AND e.`id_sediasdm` = 
a.`id_sediasdm` AND d.`id_regulasi` = a.`id_regulasi` AND c.`id_inovasijns` = 
a.`id_inovasijns` AND b.`id_unit` = a.`id_unit` AND a.no_inovasi = '$no_inovasi'

I try looking for the solution from other threat, almost all of them suggest using an index.我尝试从其他威胁中寻找解决方案,几乎所有人都建议使用索引。 But I don't understand which tables and fields should be added to the index.但是我不明白应该将哪些表和字段添加到索引中。 Please help to solve this problem.请帮助解决这个问题。

I already run this query :我已经运行了这个查询:

CREATE INDEX idx_inovasi1 ON inovasi (no_inovasi, tgl_inovasi, satuan, id_unit, nm_kelompok, subjek, id_inovasijns, id_inovasikat, id_regulasi, regulasi_doc, id_sediasdm, sediasdm_doc);

but it still hasn't affected anything.但它仍然没有影响任何东西。

您应该使用索引,检查如何索引字段,但最重要的是索引“where”之后的字段

If you add the results of the EXPLAIN (your SQL), it would be helpful to pinpoint issues.如果您添加 EXPLAIN(您的 SQL)的结果,则有助于查明问题。 https://dev.mysql.com/doc/refman/8.0/en/explain.html https://dev.mysql.com/doc/refman/8.0/en/explain.html

How about switching to left join's, using the modern JOIN syntax.如何切换到左连接,使用现代 JOIN 语法。

SELECT a.*
, LEFT(a.tgl_inovasi, 10) AS tgl_ino
, b.`nm_unit`
, c.`nm_inovasijns`
, d.`nm_regulasi`
, e.`nm_sediasdm`
, f.`nm_anggaran`
, g.`nm_it`
, h.`nm_bimtek`
, i.`nm_renstra`
, j.`nm_jejaring`
, k.`nm_replikasi`
, l.`nm_pedoman`
, m.`nm_pengelola`
, n.`nm_informasi`
, o.`nm_penyelesaian`
, p.`nm_partisipasi`
, q.`nm_kemudahaninf`
, r.`nm_kemudahanpro`
, s.`nm_online`
, t.`nm_kecepatan`
, u.`nm_kemanfaatan`
, v.`nm_kepuasan`
, w.`nm_user`
, x.nm_inovasikat 
FROM inovasi a
LEFT JOIN unit b ON b.`id_unit` = a.`id_unit` 
LEFT JOIN inovasijns c ON c.`id_inovasijns` = a.`id_inovasijns` 
LEFT JOIN regulasi d ON d.`id_regulasi` = a.`id_regulasi` 
LEFT JOIN sediasdm e ON e.`id_sediasdm` = a.`id_sediasdm` 
LEFT JOIN anggaran f ON f.`id_anggaran` = a.`id_anggaran` 
LEFT JOIN it g ON g.`id_it` = a.`id_it` 
LEFT JOIN bimtek h ON h.`id_bimtek` = a.`id_bimtek` 
LEFT JOIN renstra i ON i.`id_renstra` = a.`id_renstra` 
LEFT JOIN jejaring j ON j.`id_jejaring` = a.`id_jejaring` 
LEFT JOIN replikasi k ON k.`id_replikasi` = a.`id_replikasi` 
LEFT JOIN pedoman l ON l.`id_pedoman` = a.`id_pedoman` 
LEFT JOIN pengelola m ON m.`id_pengelola` = a.`id_pengelola` 
LEFT JOIN informasi n ON n.`id_informasi` = a.`id_informasi` 
LEFT JOIN penyelesaian o ON o.`id_penyelesaian` = a.`id_penyelesaian` 
LEFT JOIN partisipasi p ON p.`id_partisipasi` = a.`id_partisipasi` 
LEFT JOIN kemudahaninf q ON q.`id_kemudahaninf` = a.`id_kemudahaninf` 
LEFT JOIN kemudahanpro r ON r.`id_kemudahanpro` = a.`id_kemudahanpro` 
LEFT JOIN online s ON s.`id_online` = a.`id_online` 
LEFT JOIN kecepatan t ON t.`id_kecepatan` = a.`id_kecepatan` 
LEFT JOIN kemanfaatan u ON u.`id_kemanfaatan` = a.`id_kemanfaatan`
LEFT JOIN kepuasan v ON v.`id_kepuasan` = a.`id_kepuasan`
LEFT JOIN `user` w ON w.`id_user` = a.`id_user` 
LEFT JOIN inovasikat x ON x.id_inovasikat = a.id_inovasikat 
WHERE a.no_inovasi = '$no_inovasi'

As for the indexes.至于指标。 There's almost a whole alphabeth of tables linked.几乎有一个完整的表字母表链接。
Most are probably joined on the primary index.大多数可能都加入了主索引。

So look at the defenition of those tables, and discover which tables don't have their PK joined.因此,查看这些表的防御,并发现哪些表没有加入它们的 PK。
Maybe those could benefit from an index on the field used in the query.也许那些可以从查询中使用的字段上的索引中受益。

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

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