0

谁能帮我优化这个查询并帮我调整 my.cnf 文件?

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

在我重新启动服务器并且查询不在 chache 后执行大约需要 5 秒。我在以下位置有索引:表 product_feeds 到 pf_id(主)列 表 raw_feed 到 rf_row_id(主)和 rf_search_index 列

解释后我得到:

*************************** 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:

不知道为什么第一行的键为空?!我应该创建哪些索引?

我在 raw_feed 表中有 263,804 行,在 product_feeds 表中有 50 行。

MY.CNF 文件:()

[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 

谁能帮我重新配置这个文件并帮我查询?

我使用具有 10GB 根存储、1.7GB(RAM?)内存的亚马逊 aws ec2 小型实例

谢谢

4

2 回答 2

2

LIKE以百分比开头的不能在 MySQL 中使用索引。这是因为 MySQL 需要知道字符串的开头才能在其索引中查找它(这是 MySQL 执行索引方式的限制,实际上是大多数数据库产品的限制)。所以,MySQL 能做的最好的事情就是扫描raw_feed——这就是它所做的。

您需要找到另一种方法来进行搜索。一种选择是(假设您使用的是 MyISAM,它可能来自您的 my.cnf)内置全文搜索。另一个是,例如,Sphinx

此外,您的 my.cnf 设置很奇怪(很想说“疯狂”)。对于具有 1.7G RAM 的系统来说,你key_buffer的方式太大了。但是最好在 Serverfault 上询问 MySQL 调优。

于 2011-10-09T02:51:07.427 回答
0

您的 raw_feed 表应该在 rf_feed_id 上有一个索引,以优化与您的 product_feeds 表的连接......没有它,它基本上试图强制索引动态匹配 JOIN 条件,更不用说 LIKE 限定符

于 2011-10-09T02:33:23.110 回答