查询优化

发布于 2024-08-22 07:11:30 字数 1037 浏览 8 评论 0原文

在我的应用程序中,我的模块使用下表

  1. PUBLIC_APPLICATION
  2. CATEGORY_MASTER
  3. NOTIFICATION_SITE_DETAIL
  4. DIMENSION_MASTER DM
  5. PUBLIC_REGISTRATION
  6. 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

  1. PUBLIC_APPLICATION
  2. CATEGORY_MASTER
  3. NOTIFICATION_SITE_DETAIL
  4. DIMENSION_MASTER DM
  5. PUBLIC_REGISTRATION
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-08-29 07:11:30

通过使用“STRAIGHT_JOIN”,您可以告诉优化器按照您所说的进行操作。
我将通知 # 移至第一个 WHERE 子句,以便首先处理它以限制您的设置。然后,我将连接设置到其他表。我之前在查询 15+ 百万条记录的政府数据以连接 15+ 个表时遇到过这种情况,花了 20 多个小时。通过将“STRAIGHT_JOIN”添加到我已经格式良好的查询中,大约花费了 2 个小时...再次,超过 15 百万条记录并连接到超过 15 个表以获取子描述详细信息。

SELECT STRAIGHT_JOIN
        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 
            PA.NOTIFICATION_NO = ? 
        AND PA.CATEGORY_ID = CM.CATEGORY_ID
        AND PA.REGISTRATION_NO = PR.REGISTRATION_NO
        AND PA.NOTIFICATION_NO = AN.NOTIFICATION_NO
        AND PA.NOTIFY_SITE_ID = PR.NOTIFY_SITE_ID 
        AND PR.NOTIFY_SITE_ID = NSD.NOTIFY_SITE_ID
        AND NSD.DIMENSION_ID  = DM.DIMENSION_ID
        AND NSD.NOTIFY_ID = AN.NOTIFY_ID 
    LIMIT 
        ?, ?

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.

SELECT STRAIGHT_JOIN
        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 
            PA.NOTIFICATION_NO = ? 
        AND PA.CATEGORY_ID = CM.CATEGORY_ID
        AND PA.REGISTRATION_NO = PR.REGISTRATION_NO
        AND PA.NOTIFICATION_NO = AN.NOTIFICATION_NO
        AND PA.NOTIFY_SITE_ID = PR.NOTIFY_SITE_ID 
        AND PR.NOTIFY_SITE_ID = NSD.NOTIFY_SITE_ID
        AND NSD.DIMENSION_ID  = DM.DIMENSION_ID
        AND NSD.NOTIFY_ID = AN.NOTIFY_ID 
    LIMIT 
        ?, ?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文