r/DatabaseHelp • u/3Fyr • Sep 04 '15
[MySQL] Nested WHERE IN, really slow. Desperately lost.
I'm using planet.osm (only one region - Lithuania, so pretty small).
I'm already tired from searching/re-searching/reading, so I thought little discussion instead of reading old posts, could be more useful.
SELECT * FROM way_tags WHERE way_id IN (
SELECT way_id FROM way_nodes WHERE node_id IN (
SELECT node_id FROM nodes WHERE
latitude >= :minLat
and latitude <= :maxLat
and longitude >= :minLng
and longitude <= :maxLng
)
)Perhaps someone has played with OSM DB here before?
I've also read Postgre being better, but that was just posts, and as I said earlier - discussion is better, right?
1
Upvotes
3
u/Is_At_Work Sep 04 '15
Can't you just use joins here instead of inner queries? Would be much faster. Do you have indexes on way_tags.way_id and way_nodes.node_id? Also if I remember correctly, the engine can make a big difference, are you using MyISAM or InnoDB (I think InnoDB is the preferred but at one point was not the default?)
2
u/wolf2600 Sep 04 '15 edited Sep 04 '15
How large is the data set for each of your 3 queries? How big is each table?
Using the lat and lng as part of the join context rather than a where clause should improve performance (if you're dealing with large amounts of data).
Have you done an explain plan to see how much data is being accessed by each step?