我一直在尝试改进现有的Oracle数据库驱动的应用程序的查询时间,该应用程序运行有点迟钝。应用程序执行几个大型查询,例如下面的一个,可能需要一个小时才能运行。在查询中使用GROUP BY子句替换DISTINCT将执行时间从100分钟缩短到10秒。我的理解是,SELECT DISTINCT和GROUP BY以相同的方式运行。为什么执行时间之间如此巨大的差距?在后端如何执行查询有什么区别?有没有一种情况,SELECT DISTINCT运行速度更快?
注意:在以下查询中,WHERE TASK_INVENTORY_STEP.STEP_TYPE =’TYPE A’仅表示可以过滤结果的多种方法之一。提供了此示例以显示加入所有没有列中包含列的表的原因,并将导致所有可用数据的十分之一
sql使用DISTINCT:
SELECT DISTINCT ITEMS.ITEM_ID,ITEMS.ITEM_CODE,ITEMS.ITEMTYPE,ITEM_TRANSACTIONS.STATUS,(SELECT COUNT(PKID) FROM ITEM_PARENTS WHERE PARENT_ITEM_ID = ITEMS.ITEM_ID ) AS CHILD_COUNT FROM ITEMS INNER JOIN ITEM_TRANSACTIONS ON ITEMS.ITEM_ID = ITEM_TRANSACTIONS.ITEM_ID AND ITEM_TRANSACTIONS.FLAG = 1 LEFT OUTER JOIN ITEM_MetaDATA ON ITEMS.ITEM_ID = ITEM_MetaDATA.ITEM_ID LEFT OUTER JOIN JOB_INVENTORY ON ITEMS.ITEM_ID = JOB_INVENTORY.ITEM_ID LEFT OUTER JOIN JOB_TASK_INVENTORY ON JOB_INVENTORY.JOB_ITEM_ID = JOB_TASK_INVENTORY.JOB_ITEM_ID LEFT OUTER JOIN JOB_TASKS ON JOB_TASK_INVENTORY.TASKID = JOB_TASKS.TASKID LEFT OUTER JOIN JOBS ON JOB_TASKS.JOB_ID = JOBS.JOB_ID LEFT OUTER JOIN TASK_INVENTORY_STEP ON JOB_INVENTORY.JOB_ITEM_ID = TASK_INVENTORY_STEP.JOB_ITEM_ID LEFT OUTER JOIN TASK_STEP_INFORMATION ON TASK_INVENTORY_STEP.JOB_ITEM_ID = TASK_STEP_INFORMATION.JOB_ITEM_ID WHERE TASK_INVENTORY_STEP.STEP_TYPE = 'TYPE A' ORDER BY ITEMS.ITEM_CODE
sql使用GROUP BY:
SELECT ITEMS.ITEM_ID,(SELECT COUNT(PKID) FROM ITEM_PARENTS WHERE PARENT_ITEM_ID = ITEMS.ITEM_ID ) AS CHILD_COUNT FROM ITEMS INNER JOIN ITEM_TRANSACTIONS ON ITEMS.ITEM_ID = ITEM_TRANSACTIONS.ITEM_ID AND ITEM_TRANSACTIONS.FLAG = 1 LEFT OUTER JOIN ITEM_MetaDATA ON ITEMS.ITEM_ID = ITEM_MetaDATA.ITEM_ID LEFT OUTER JOIN JOB_INVENTORY ON ITEMS.ITEM_ID = JOB_INVENTORY.ITEM_ID LEFT OUTER JOIN JOB_TASK_INVENTORY ON JOB_INVENTORY.JOB_ITEM_ID = JOB_TASK_INVENTORY.JOB_ITEM_ID LEFT OUTER JOIN JOB_TASKS ON JOB_TASK_INVENTORY.TASKID = JOB_TASKS.TASKID LEFT OUTER JOIN JOBS ON JOB_TASKS.JOB_ID = JOBS.JOB_ID LEFT OUTER JOIN TASK_INVENTORY_STEP ON JOB_INVENTORY.JOB_ITEM_ID = TASK_INVENTORY_STEP.JOB_ITEM_ID LEFT OUTER JOIN TASK_STEP_INFORMATION ON TASK_INVENTORY_STEP.JOB_ITEM_ID = TASK_STEP_INFORMATION.JOB_ITEM_ID WHERE TASK_INVENTORY_STEP.STEP_TYPE = 'TYPE A' GROUP BY ITEMS.ITEM_ID,ITEM_TRANSACTIONS.STATUS ORDER BY ITEMS.ITEM_CODE