1

有人可以告诉我为什么“CASE WHEN”让它这么慢以及如何优化/修复它吗?

需要将固定在结果中的项目按顺序排列在首位。

我可能可以在 sql 查询之后执行此操作,但我认为如果正确完成,如果在 sql 查询中完成此排序会更快。

慢查询~490ms

SELECT 
    places.id AS place_id,
    url,
    title,
    thumbnails.score AS score,
    thumbnails.clipping AS clipping,
    thumbnails.lastModified AS lastModified,
    EXISTS (SELECT 1 FROM pinned pi WHERE pi.place_id = places.id) AS pinned
FROM places
    LEFT JOIN thumbnails ON (thumbnails.place_id = places.id)
    LEFT JOIN pinned j ON (j.place_id = places.id) WHERE (hidden == 0)
ORDER BY case when j.id is null then 1 else 0 end,
    j.id,
    frecency DESC LIMIT 24

删除“CASE WHEN”部分:查询 ~6ms

SELECT
    places.id AS place_id,
    url,
    title,
    thumbnails.score AS score,
    thumbnails.clipping AS clipping,
    thumbnails.lastModified AS lastModified,
    EXISTS (SELECT 1 FROM pinned pi WHERE pi.place_id = places.id) AS pinned
FROM places
    LEFT JOIN thumbnails ON (thumbnails.place_id = places.id) WHERE (hidden == 0)
ORDER BY frecency DESC LIMIT 24

表信息:

var Create_Table_Places =
    'CREATE TABLE places (' +
        'id INTEGER PRIMARY KEY,' +
        'url LONGVARCHAR,' +
        'title LONGVARCHAR,' +
        'visit_count INTEGER DEFAULT 0,' +
        'hidden INTEGER DEFAULT 0 NOT NULL,' +
        'typed INTEGER DEFAULT 0 NOT NULL,' +
        'frecency INTEGER DEFAULT -1 NOT NULL,' +
        'last_visit_date INTEGER,' +
        'dateAdded INTEGER,' +
        'lastModified INTEGER' +
    ')';

var Create_Table_Thumbnails =
    'CREATE TABLE thumbnails (' +
        'id INTEGER PRIMARY KEY,' +
        'place_id INTEGER UNIQUE,' +
        'data LONGVARCHAR,' +
        'score REAL,' +
        'clipping INTEGER,' +
        'dateAdded INTEGER,' +
        'lastModified INTEGER' +
    ')';

var Create_Table_Pinned =
    'CREATE TABLE pinned (' +
        'id INTEGER PRIMARY KEY,' +
        'place_id INTEGER UNIQUE,' +
        'position INTEGER,' +
        'dateAdded INTEGER,' +
        'lastModified INTEGER' +
    ')';
4

1 回答 1

1

要了解查询的执行是否存在根本差异,请使用EXPLAIN QUERY PLAN. 在 SQLite 3.7.almost15 中,您的查询有以下计划:

selectid order from detail
-------- ----- ---- ------
0        0     0    SCAN TABLE places (~100000 rows)
0        1     1    SEARCH TABLE thumbnails USING INDEX sqlite_autoindex_thumbnails_1 (place_id=?) (~1 rows)
0        2     2    SEARCH TABLE pinned AS j USING COVERING INDEX sqlite_autoindex_pinned_1 (place_id=?) (~1 rows)
0        0     0    EXECUTE CORRELATED SCALAR SUBQUERY 1
1        0     0    SEARCH TABLE pinned AS pi USING COVERING INDEX sqlite_autoindex_pinned_1 (place_id=?) (~1 rows)
0        0     0    USE TEMP B-TREE FOR ORDER BY

selectid order from detail
-------- ----- ---- ------
0        0     0    SCAN TABLE places (~100000 rows)
0        1     1    SEARCH TABLE thumbnails USING INDEX sqlite_autoindex_thumbnails_1 (place_id=?) (~1 rows)
0        0     0    EXECUTE CORRELATED SCALAR SUBQUERY 1
1        0     0    SEARCH TABLE pinned AS pi USING COVERING INDEX sqlite_autoindex_pinned_1 (place_id=?) (~1 rows)
0        0     0    USE TEMP B-TREE FOR ORDER BY

这两个计划几乎相同,除了重复pinned查找。如果您的 SQLite 不以这种方式执行查询,请更新它。

在您的第一个查询中,您可以删除该pinned字段的子查询,因为您已经加入了pinned表,并且您正在执行与为连接完成的查找完全相同的查找;改为使用j.id IS NOT NULL

您的目的是在其他值之后CASE WHEN对 s 进行排序。NULL您可以通过将所有NULLs转换为在 numbers 之后排序的某个值来获得相同的效果,例如字符串:

... ORDER BY IFNULL(j.id, ''), frecency DESC

但是,理论上,这与CASE WHEN.

于 2012-10-23T19:36:23.907 回答