查询优化
在我的应用程序中,我的模块使用下表
- PUBLIC_APPLICATION
- CATEGORY_MASTER
- NOTIFICATION_SITE_DETAIL
- DIMENSION_MASTER DM
- PUBLIC_REGISTRATION
- ALLOTMENT_NOTIFICATION。
从下表中我检索数据
SELECT PA.REGISTRATION_NO,PA.APP_ID,PA.NO_OF_ATTEMPTS,CM.CATEGORY_NAME,
DM.SITE_DIMENSION,PR.BDA_NO,AN.NOTIFY_ID
FROM **PUBLIC_APPLICATION PA,CATEGORY_MASTER CM,NOTIFICATION_SITE_DETAIL NSD,DIMENSION_MASTER DM, PUBLIC_REGISTRATION PR,ALLOTMENT_NOTIFICATION AN**
WHERE **CM.CATEGORY_ID = PA.CATEGORY_ID AND
NSD.NOTIFY_SITE_ID = PR.NOTIFY_SITE_ID AND
DM.DIMENSION_ID = NSD.DIMENSION_ID AND
PR.REGISTRATION_NO = PA.REGISTRATION_NO AND
AN.NOTIFICATION_NO = PA.NOTIFICATION_NO AND
PR.NOTIFY_SITE_ID = PA.NOTIFY_SITE_ID AND NSD.NOTIFY_ID = AN.NOTIFY_ID AND
PA.NOTIFICATION_NO = ?** LIMIT ?, ?
PUBLIC_APPLICATION & PUBLIC_REGISTRATION数据量很大,有近i百万条记录,其他表有5000条左右的记录。
如果我执行上述查询,需要超过 30 分钟才能获得结果,任何人都可以建议我编写有效的查询以在最短的时间内获得结果。
忘了指定,我正在使用 mysql 数据库。
In my application, my module uses the following tables
- PUBLIC_APPLICATION
- CATEGORY_MASTER
- NOTIFICATION_SITE_DETAIL
- DIMENSION_MASTER DM
- PUBLIC_REGISTRATION
- ALLOTMENT_NOTIFICATION.
From the following tables I am retrieving the data
SELECT PA.REGISTRATION_NO,PA.APP_ID,PA.NO_OF_ATTEMPTS,CM.CATEGORY_NAME,
DM.SITE_DIMENSION,PR.BDA_NO,AN.NOTIFY_ID
FROM **PUBLIC_APPLICATION PA,CATEGORY_MASTER CM,NOTIFICATION_SITE_DETAIL NSD,DIMENSION_MASTER DM, PUBLIC_REGISTRATION PR,ALLOTMENT_NOTIFICATION AN**
WHERE **CM.CATEGORY_ID = PA.CATEGORY_ID AND
NSD.NOTIFY_SITE_ID = PR.NOTIFY_SITE_ID AND
DM.DIMENSION_ID = NSD.DIMENSION_ID AND
PR.REGISTRATION_NO = PA.REGISTRATION_NO AND
AN.NOTIFICATION_NO = PA.NOTIFICATION_NO AND
PR.NOTIFY_SITE_ID = PA.NOTIFY_SITE_ID AND NSD.NOTIFY_ID = AN.NOTIFY_ID AND
PA.NOTIFICATION_NO = ?** LIMIT ?, ?
PUBLIC_APPLICATION & PUBLIC_REGISTRATION have large number of data, nearly i Million records and other tables have around 5000 records.
If I execute the above the query it takes more than 30 min to get the results, Can any one suggest me to write the effiecient query to get the results within minimum time.
forgot to specify, I am using the mysql database for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用“STRAIGHT_JOIN”,您可以告诉优化器按照您所说的进行操作。
我将通知 # 移至第一个 WHERE 子句,以便首先处理它以限制您的设置。然后,我将连接设置到其他表。我之前在查询 15+ 百万条记录的政府数据以连接 15+ 个表时遇到过这种情况,花了 20 多个小时。通过将“STRAIGHT_JOIN”添加到我已经格式良好的查询中,大约花费了 2 个小时...再次,超过 15 百万条记录并连接到超过 15 个表以获取子描述详细信息。
By using "STRAIGHT_JOIN", you tell the optimizer to do it as you say so.
I moved the Notification # as the first WHERE clause so it gets handled first to limit your set. THEN, I set the joins to the other tables. I had this before when querying gov't data of 15+ million records to join 15+ tables and it took 20+ hours. By adding just the "STRAIGHT_JOIN" to my already well-formed query, it took about 2 hours ... again, 15+ million records and join to over 15 tables for child descriptive details.