Mysql大记录表插入问题
我是一名开发人员,在管理包含大量记录的表时遇到问题。
我正在执行一个 cron 作业来填充主表(表 A)中的数据,该表有 5-6 列和大约 4,00,000 到 5,00,000 行,然后创建另一个表,并且该表中的数据将随着时间的推移继续增加。
表 A 包含原始数据,我的输出表是表 B
我的 cron 脚本截断表 B 中的数据,然后使用选择查询插入数据
TRUNCATE TABLE_B;
INSERT INTO TABLE_B (field1, field2)
SELECT DISTINCT(t1.field2), t2.field2
FROM TABLE_A AS t1
INNER JOIN TABLE_A t2 ON t2.field1=t1.field1
WHERE t1.field2 <> t2.field2
GROUP BY t1.field2, t2.field2
ORDER BY COUNT(t1.field2) DESC;
上面的选择查询生成大约 1,50,000 到 2,00,000 行
现在填充表 B 需要太多时间同时,如果我的应用程序尝试访问表 B,则选择查询失败
解释查询结果如下:
'1','PRIMARY','T1','ALL','field1_index',NULL,NULL,NULL,'431743','Using temporary;Using filesort'
'1','PRIMARY','T2','ref','field1_index','field1_index','767','DBNAME.T1.field1','1','Using where'
有人可以帮助我改进此过程,或指导我上述过程的替代方案吗?
谢谢苏克
图
I am a developer and I am facing an issue while managing table which has large amount of records.
I am executing a cron job to fill up data in primary table (Table A) which has 5-6 columns and approx 4,00,000 to 5,00,000 rows and then creating another table and data in this table would continue to increase over the time.
TABLE A contains the raw data and my output table is TABLE B
My cron script truncates data in Table B then inserts data using select query
TRUNCATE TABLE_B;
INSERT INTO TABLE_B (field1, field2)
SELECT DISTINCT(t1.field2), t2.field2
FROM TABLE_A AS t1
INNER JOIN TABLE_A t2 ON t2.field1=t1.field1
WHERE t1.field2 <> t2.field2
GROUP BY t1.field2, t2.field2
ORDER BY COUNT(t1.field2) DESC;
Above select query produces approx 1,50,000 to 2,00,000 rows
Now it takes too much time to populate TABLE B and meanwhile If my application tries to access TABLE B then select query fails
Explaining query results following:
'1','PRIMARY','T1','ALL','field1_index',NULL,NULL,NULL,'431743','Using temporary;Using filesort'
'1','PRIMARY','T2','ref','field1_index','field1_index','767','DBNAME.T1.field1','1','Using where'
Can someone please help me in improving this process, or guide me alternatives for above process?
Thanks
Suketu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在存储过程中完成整个过程。
不要截断这么大的表。请执行以下步骤:
You should do the whole process in a stored proc.
Do not truncate such a large table. Follow the following steps:
根据我的观点,解决方案将是这样的:
例如文件为“C:\TEMP\DATA1.SQL”。此查询会发生什么,使用 TAB 分隔符创建一个简单的新文件以插入到任何表中。
现在如何将数据导入到表中。
通过此查询,将插入数据,另一方面,您将能够使用要在其中插入数据的表。
According to my view the solution would be like this:
For instance file as "C:\TEMP\DATA1.SQL". What will happen with this query a simple new file is created with TAB delimiter to insert into any table.
Now how to import the data to table.
With this query the data will be inserted and on the other hand you will be able to use the table in which you are inserting the data.