I’ve been trying to find out why my SQLite database is performing relatively slowly (4 seconds to insert 1500 records) and I think I’ve narrowed it down to this query. Is there a way to optimise this?
"INSERT OR REPLACE INTO MainFrame(WID,PName,PAlias,PModel,FriendID, UniverseID, GalaxyID) VALUES
((SELECT WID FROM Worlds WHERE WName= ?),
@pname,
@palias,
@pmodel,
(SELECT FriendID FROM Friend WHERE FriendName = @eFriend),
(SELECT UniverseID FROM Universes WHERE UniverseName = @eUniverse),
(SELECT GalaxyID FROM Galaxies WHERE GalaxyName = @eGalaxy ))";
As you can see, there are a few Selects being used in an insert query. The reason for this is because the loop inserts data into other tables (WID, FriendID, UniverseID, GalaxyID) so I don’t have that data until it’s been inserted. I need this data to insert into the MainFrame table but this feels like a brute force approach. Any advice?
Have you narrowed it down to which part of the query is the problem? ie have you run the select on its own to see how quickly it returns. If the select is slow, maybe look at indexes. If select is quick maybe its indexes on the MainFrame table that’s slowing insertion.