非常慢的简单sql查询

时间:2018-03-12 13:32:22

标签: mysql sql innodb

我在InnodDB中有5个表,它们有大约4千万条记录

我想为所有表格搜索contact_id创建简单的过滤器。

为此,我创建了以下查询:

SELECT c.id FROM contacts AS c 
JOIN bonus_cards AS B USING(client_id) 
JOIN orders AS o USING(card_number) 
JOIN order_items AS oi USING (order_id) 
JOIN products AS p USING (product_id) WHERE p.brand_id = 125; 

我还使用了不同的查询样式:

SELECT c.id FROM contacts AS c 
INNER JOIN bonus_cards AS bc ON(bc.client_id=c.client_id) 
INNER JOIN orders AS o ON(bc.card_number=o.card_number) 
INNER JOIN order_items AS oi ON(o.order_id=oi.order_id) 
INNER JOIN products AS p ON(oi.product_id=p.product_id) WHERE p.brand_id = 125;

时间没有区别。

请求很长。而且很长一段时间都超过30分钟。

我有所有必要的索引,当我进行查询查询时,我看到索引都参与其中。

我的配置my.cnf:

    net_read_timeout = 3600
    net_write_timeout = 3600
    wait_timeout = 120
    interactive_timeout = 120
    key_buffer_size =32M
    sort_buffer_size = 8M
    max_allowed_packet = 1M
    read_rnd_buffer_size = 1M
    thread_stack = 128K
    query_cache_limit = 1M
    query_cache_size = 0
    query_cache_type = 1
    thread_cache_size = 16
    max_heap_table_size = 128M
    tmp_table_size = 128M
    innodb_open_files = 4096
    innodb_file_per_table = 1
    innodb_flush_method=O_DIRECT
    innodb_flush_log_at_trx_commit=0
    innodb_log_file_size = 128M
    innodb_log_buffer_size = 16M
    innodb_buffer_pool_size=2048M
    innodb_buffer_pool_instances=1
    #innodb_additional_mem_pool_size = 16M
    innodb_thread_concurrency = 16
    innodb_read_io_threads = 8
    innodb_write_io_threads = 8
    innodb_stats_on_metadata = 0
    innodb_data_file_path=ibdata1:10M:autoextend

2 个答案:

答案 0 :(得分:0)

对于此查询:

SELECT c.id
FROM contacts AS c JOIN 
     bonus_cards AS B USING(client_id) JOIN 
     orders AS o USING(card_number) JOIN
     order_items AS oi USING (order_id) JOIn
     products AS p USING (product_id)
WHERE p.brand_id = 125; 

最佳指标是:

  • products(brand_id, product_id)
  • order_items(product_id, order_id)
  • orders(order_id, card_number)
  • bonus_cards(card_number, client_id)
  • contacts(client_id, id)

那就是说,我很惊讶您必须通过bonus_cards这样的表来与订单联系。

答案 1 :(得分:0)

我在db结构中发现了bug。我正在使用我收到的数据库,并且我的完整性没有任何变化。

但这句话 - 从不相信数据,结果证明是真的。数据库不是链接子表的正确数据类型。

例如:

contacts - client_id int 8 not null,
bonus_cards - client_id int 8 not null,
bonus_cards - card_number varchar 56 not null,
orders - card_number varchar 56 not null,
orders - order_id int 11 not null,
order_items order_id **varchar 50** not null,
order_items product_id **varchar 50** not null,
products product_id int 8 not null,
products brand_id int 8 not null,

我可能还有一个例子,可能来自开发人员。 首先,在调试查询时,请检查数据类型。

我希望我帮助社区。

P.S。查询执行速度5s(8.2Gb数据库和2GB RAM):)简单

相关问题