sqlite query to get earliest date

Mike

I have the following query:

  SELECT (CASE WHEN cc.id IS NULL THEN dc.id ELSE cc.id END) AS id,
 l.CALL,l.QSO_DATE,cc.prefix,l.state,l.QSL_RCVD,l.Band,l.Mode,
(CASE  WHEN CC.Country IS NULL THEN dc.Country ELSE cc.Country END) AS country,
(CASE  WHEN CC.Country IS NULL THEN 'Y' ELSE 'N' END) AS 'deleted',
(CASE  WHEN CC.flags IS NULL THEN dc.flags ELSE cc.flags END) AS 'flag'
FROM lotw l
LEFT OUTER JOIN CountryCode AS cc on cc.code = l.dxcc 
LEFT OUTER JOIN DeleteCountry AS dc on dc.code = l.dxcc 
WHERE l.DXCC > '' 

GROUP BY CC.Country,dc.Country  ORDER BY cc.COUNTRY,dc.COUNTRY.

which returns a set of unique country that matches the countrycode table code field. the speed is good less than a second.

I needed to expand this so the query select's the earliest QSLRDATE from the LOTW table. so I modify the query like this:

SELECT (CASE WHEN cc.id IS NULL THEN dc.id ELSE cc.id END) AS id, l.CALL,l.QSO_DATE,cc.prefix,l.state,l.QSL_RCVD,l.Band,l.Mode,
(CASE  WHEN CC.Country IS NULL THEN dc.Country ELSE cc.Country END) AS country,
(CASE  WHEN CC.Country IS NULL THEN 'Y' ELSE 'N' END) AS 'deleted',
(CASE  WHEN CC.flags IS NULL THEN dc.flags ELSE cc.flags END) AS 'flag'
FROM lotw l
LEFT OUTER JOIN CountryCode AS cc on cc.code = l.dxcc 
LEFT OUTER JOIN DeleteCountry AS dc on dc.code = l.dxcc 
WHERE l.QSLRDATE = (SELECT MIN(l2.QSLRDATE) FROM LOTW l2 WHERE l2.DXCC=l.DXCC) AND l.DXCC > '' 

GROUP BY CC.Country,dc.Country  ORDER BY cc.COUNTRY,dc.COUNTRY

this works but the performance went from less than a second to 15 seconds. The sub query I added:

l.QSLRDATE = (SELECT MIN(l2.QSLRDATE) FROM LOTW l2 WHERE l2.DXCC=l.DXCC)

can't be the best way to accomplish what I want. any help would be great.

MatBailie

Try joining on a sub-query rather than using a correlated sub-query in the WHERE clause.

SELECT
  (CASE WHEN cc.id IS NULL THEN dc.id ELSE cc.id END) AS id,
  l.CALL,l.QSO_DATE,cc.prefix,l.state,l.QSL_RCVD,l.Band,l.Mode,
  (CASE  WHEN CC.Country IS NULL THEN dc.Country ELSE cc.Country END) AS country,
  (CASE  WHEN CC.Country IS NULL THEN 'Y' ELSE 'N' END) AS 'deleted',
  (CASE  WHEN CC.flags IS NULL THEN dc.flags ELSE cc.flags END) AS 'flag'
FROM
  (SELECT dxcc, MIN(qslrdate) AS qslrdate FROM lotw GROUP BY dxcc) AS qslr
     INNER JOIN lotw AS l ON l.dxcc = qslr.dxcc AND l.qslrdate = qslr.qslrdate
LEFT OUTER JOIN CountryCode AS cc on cc.code = l.dxcc 
LEFT OUTER JOIN DeleteCountry AS dc on dc.code = l.dxcc 
WHERE l.DXCC > ''
GROUP BY CC.Country,dc.Country  ORDER BY cc.COUNTRY,dc.COUNTRY

Also, make sure that lotw has an index on (dxcc, qslrdate) to make ensure both the sub-query and join perform well.

Infact, ensure you have that index first, and retry your query. Then, if the performance is still poor, try the above query.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I get the earliest record by date for a unique constraint?

分類Dev

How to get current date time format in SQlite?

分類Dev

SQLite: Get week number from date

分類Dev

How to write a sqlite query to get specific data?

分類Dev

Get truth value of column value in sqlite query

分類Dev

SQLite query to get data from multiple tables

分類Dev

Earliest date after current date without associated record

分類Dev

Issue integrating SQLITE3 date query into MYSQL

分類Dev

SQL Server : query to get MINUTE of date in specific date

分類Dev

how to get the oldest / earliest year value from a class

分類Dev

Postgres query to get date and total amount grouped by day

分類Dev

SQL query to get free vehicles on basis of their booking date

分類Dev

Date in SQLite not working

分類Dev

Working with date in sqlite on android

分類Dev

SQLite Slow Select Query

分類Dev

SQLite query > operator not working

分類Dev

Android:SQLITE Select Query

分類Dev

How can I get a user's rank/position in SQLite3 with a query?

分類Dev

Sqlite query to get next avaiable character of search text which contains in firstname or lastname

分類Dev

AWS dynamoDB query by date

分類Dev

Mongoengine query with date range

分類Dev

Date Difference Query

分類Dev

How can I get the right day of the week, if I am setting the date in relation to the given query string parameters?

分類Dev

Adobe cq5 query builder - get multiple properties within a date range

分類Dev

python sqlite3 query with AND

分類Dev

How to use wildcards in a SQLite query?

分類Dev

Sqlite Distinct Query From -To、To --From

分類Dev

MySQL query does not run in SQLite

分類Dev

SQLite query to GROUP BY nearby row

Related 関連記事

ホットタグ

アーカイブ