SQL 查询性能对于 MySQL 来说太差了
我在 MySQL 平台上运行以下 SQL 查询。
表 A 是一个具有单列(主键)和 25K 行的表。 表 B 有几列和 75K 行。
执行以下查询需要 20 分钟。如果您能提供帮助,我会很高兴。
INSERT INTO sometable
SELECT A.PrimaryKeyColumn as keyword, 'SomeText', B.*
FROM A, B
WHERE B.PrimaryKeyColumn = CONCAT(A.PrimaryKeyColumn, B.NotUniqueButIndexedColumn);
I run the following SQL Query on a MySQL platform.
Table A is a table which has a single column (primary key) and 25K rows.
Table B has several columns and 75K rows.
It takes 20 minutes to execute following query. I will be glad if you could help.
INSERT INTO sometable
SELECT A.PrimaryKeyColumn as keyword, 'SomeText', B.*
FROM A, B
WHERE B.PrimaryKeyColumn = CONCAT(A.PrimaryKeyColumn, B.NotUniqueButIndexedColumn);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行不带
INSERT
的SELECT
,看看问题是否出在SELECT
上。如果是使用
SELECT
,则按照 MySQL 文档解释了如何使用EXPLAIN
优化查询。如果
SELECT
运行正常,但INSERT
需要很长时间,请确保sometable
上没有大量不必要的索引。除此之外,您可能需要进行一些 MySQL 调整和/或操作系统调整(例如内存或磁盘性能),以通过INSERT
获得可测量的性能提升。Run the
SELECT
without theINSERT
to see if the problem is with theSELECT
or not.If it is with the
SELECT
, follow the MySQL documentation explaining how to optimize queries usingEXPLAIN
.If the
SELECT
runs fine but theINSERT
takes forever, make sure you don't have a lot of unnecessary indexes onsometable
. Beyond that, you may need to do some MySQL tuning and/or OS tuning (e.g., memory or disk performance) to get a measurable performance boost with theINSERT
.如果我猜对了,你大致会尝试插入 18.75 亿条记录 - (与 where 子句不匹配)。
20分钟听起来也不算太糟糕......
If I get it right you are roughly trying to insert 1.875 Billion records - (which does not match the where clause).
For that 20 minutes doesn't sound too bad....