I'm just starting to learn MySQL and I have a script that loads a large CSV file (4 million rows) into a table and then adds a spatial index to the GeometryLatLon column. The spatial index creation takes around 4 hours to complete. However, when I perform the same operation on SQLite, the spatial index creation only takes around 10 minutes. Is this performance difference normal?
Here's the MySQL script I'm using:
DROP TABLE IF EXISTS uprns;
CREATE TABLE uprns (
UPRN BIGINT NOT NULL,
X_COORDINATE DOUBLE,
Y_COORDINATE DOUBLE,
LATITUDE DOUBLE,
LONGITUDE DOUBLE,
GeometryLatLon POINT NOT NULL
) ENGINE=InnoDB;
-- Load data from CSV
LOAD DATA INFILE '4_million_rows.csv'
INTO TABLE uprns
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(UPRN, X_COORDINATE, Y_COORDINATE, LATITUDE, LONGITUDE)
SET GeometryLatLon = ST_GeomFromText(CONCAT('POINT(', LONGITUDE, ' ', LATITUDE, ')'), 4326);
ALTER TABLE uprns MODIFY GeometryLatLon POINT NOT NULL SRID 4326;
-- ALTER TABLE uprns DROP INDEX GeometryLatLon;
ALTER TABLE uprns ADD SPATIAL INDEX (GeometryLatLon);
CREATE INDEX idx_uprns_UPRN ON uprns (UPRN);
Are there any potential performance optimizations I can make in my MySQL script to improve the speed of spatial index creation? Or are there any known differences in the way MySQL and SQLite handle spatial indexing that could explain this disparity in performance?
ALTER TABLE uprns ADD SPATIAL INDEX (GeometryLatLon);. Unless I’m missing something? Once this line is executed, spatial queries are much faster.