我有一个 MySQL 数据库,我想执行更大的搜索。
我在其中一张表中有大约 10k 条记录,预计会增长,但速度很慢。
最大的问题是,要执行搜索,我必须使用 4 个 JOINS 进行查询,我认为这会导致搜索速度变慢。
所以这里有一些示例结构:
[table records]
id INT unsigned PRIMARY KEY auto_increment
description text
label INT unsigned
type INT unsigned
price DECIMAL
[table records_labels]
id INT unsigned PRIMARY KEY auto_increment
label varchar
[table records_types]
id INT unsigned PRIMARY KEY auto_increment
type varchar
[table records_serial]
id INT unsigned PRIMARY KEY auto_increment
serial varchar
record INT unsigned
[table records_barcode]
id INT unsigned PRIMARY KEY auto_increment
barcode varchar
record INT unsigned
所以事情是这样运行的:
我运行一个选择的查询records.id, records.description, records.price, records_labels.label, records_types.type, records_serial.serial, records_barcode.barcode;
所以完整的查询是这样的:
SELECT records.id, records.description, records.price, records_labels.label, records_types.type, records_serial.serial, records_barcode.barcode FROM records JOIN records_labels ON records_labels.id = records.label JOIN records_types ON records_types.id = records.type LEFT JOIN records_serial ON records_serial.record = record.id LEFT JOIN records_barcode ON records_barcode.record = record.id WHERE records_serial.serial LIKE %SEARCH_TERM% OR records_barcode.barcode LIKE %SEARCH_TERM%
我认为这里的解决方案是索引,但我对它不是很熟悉。
那么很快,如何加速和优化这种查询呢?