我到处都解释说我应该在这样的情况下创建一个表索引:
SELECT * FROM foo, bar WHERE foo.field1 < <some_value>;
不过我的情况不一样。
我正在使用 SQLite 并具有以下架构:
CREATE TABLE leagues(
id integer primary key,
...
);
CREATE TABLE players(
playerid integer,
id integer,
type integer,
value double,
PRIMARY KEY(playerid,id)
);
CREATE TABLE scoretype(
scoreid integer primary key,
scorename varchar(50)
);
CREATE TABLE leaguescoretype(
playerid integer,
id integer,
scoreid integer,
value double,
foreign key(playerid) references players(playerid),
foreign key(id) references leagues(id),
foreign key(scoreid) references scoretype(scoreid)
);
我想运行以下查询:
SELECT
players.playerid,
scoretype.scorename,
leaguescoretype.value
FROM players,scoretype,leaguescoretype
WHERE scoretype.scoreid = leaguescoretype.scoreid
AND players.playerid = leaguescoretype.playerid
AND players.playerid = 1 AND players.id = 1;
尝试在 EXPLAIN QUERY PLAN 下运行此查询,我得到:
SCAN TABLE leaguescoretype (~1000000 rows)
SEARCH TABLE players USING COVERING INDEX sqlite_autoindex_players(id=? AND playerid=?) (~1 rows)
SEARCH TABLE scoretype USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
有没有办法提高这个查询的性能?