Optimize MySQL Query, reconfigure MY.CNF?
-
07-02-2021 - |
Pergunta
can anyone can help me optimize this query and help me to tune my my.cnf file?
SELECT
rf_row_id,
pf_id,
pf_wordpress_url,
pf_merchant_logo,
rf_desc,
rf_manufacturer,
rf_product_name,
rf_small_image,
rf_price,
pf_name,
rf_shipping_handling_cost,
pf_voucher_code_expiry_date,
pf_voucher_code,
pf_voucher_code_instructions,
rf_last_modified,
pf_last_update,
rf_deep_link,
pf_delivery_free_from,
pf_voucher_code_url,
pf_delivery_string,
pf_delivery_fee_2
FROM raw_feed, product_feeds
WHERE pf_id=rf_feed_id
AND ( rf_search_index LIKE '%C4838AE%' OR rf_search_index LIKE '%HP11%')
GROUP BY rf_feed_id, rf_product_id ORDER BY rf_price ASC
It takes around 5 sec to execute after i restart server and query isn't in chache. I have indexes at: table product_feeds to pf_id (Primary) column table raw_feed to rf_row_id (Primary) and rf_search_index column
After EXPLAIN I get:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: raw_feed
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 263804
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: product_feeds
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 2
ref: usrdb_rnbx.raw_feed.rf_feed_id
rows: 1
Extra:
Don't know why key for first row is null?! What indexes should I create?
I have 263,804 total rows in raw_feed table and 50 rows in product_feeds table.
MY.CNF file: ()
[mysqld]
basedir=/opt/bitnami/mysql
character-set-server=UTF8
collation-server=utf8_general_ci
datadir=/opt/bitnami/mysql/data
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
tmpdir=/opt/bitnami/mysql/tmp
set-variable = max_connections=3096
set-variable = max_allowed_packet=15M
key_buffer = 1024M
key_buffer_size = 1024M
table_cache = 1024
sort_buffer_size = 50M
read_buffer_size = 50M
read_rnd_buffer_size = 16M
myisam_sort_buffer_size = 3M
thread_cache = 32
thread_concurrency = 16
open-files-limit= 261424
set-variable = thread_stack=512k
set-variable = query_cache_size=128M
set-variable = wait_timeout=120
set-variable = interactive_timeout=60
set-variable = max_connect_errors=999999
Can anyone help me to reconfigure this file and help me with query?
I use amazon aws ec2 small instance with 10GB root storage, 1.7GB (RAM?) memory
Thanks
Solução
LIKE
with a percent at the beginning can't use an index in MySQL. This is because MySQL needs to know the start of the string in order to look it up in its index (thats a limitation of how MySQL does indices, and actually of most database products). So, the best MySQL can do is scan raw_feed
—which is what its doing.
You need to find another way to do that search. One option would be (assuming you're using MyISAM, which is probably right from your my.cnf) built-in fulltext search. Another would be, e.g., Sphinx.
Also, your my.cnf settings are weird (tempted to say "insane"). Your key_buffer
is way to large for a system with 1.7G of RAM. But MySQL tuning is probably best asked on Serverfault.
Outras dicas
Your raw_feed table should have an index on rf_feed_id to optimize the join to your product_feeds table... without that, its basically trying to force an index on the fly to match the JOIN condition, let alone the LIKE qualifier