I have the following table with 1 billion records.
create table PfTest
(
cola int,
colb int,
colc date,
cold varchar(10),
ID int
);
Now I want to display the records which are in a specific dates and not in specific dates.
For which I am using the following 2 types of queries:
Query 1:
select DISTINCT cola, colb, colc, cold, ID
from PfTest
WHERE colc In ('2014-01-01')
AND cold NOT IN (SELECT cold
FROM PfTest
WHERE ID = 1
AND colc IN ('2014-01-02', '2014-01-03',
'2014-01-04', '2014-01-05', '2014-01-06'));
Query 2:
WITH cte AS
(
SELECT DISTINCT cola, colb, colc, cold, ID
FROM PfTest
WHERE cold NOT IN (SELECT cold FROM PfTest
WHERE ID = 1
AND colc IN('2014-01-02', '2014-01-03',
'2014-01-04', '2014-01-05', '2014-01-06'))
)
SELECT cola, colb, colc, cold, ID
FROM cte
WHERE colc IN ('2014-01-01');
Above both query plans are same for execution. And both are taking huge time for execution. Can I write some better query for this situation?