mysql 处理大数据太慢_MySQL查询大数据速度缓慢

I'm no MySQL whiz but I get it, I have just inherited a pretty large table (600,000 rows and around 90 columns (Please kill me...)) and I have a smaller table that I've created to link it with a categories table.

I'm trying to query said table with a left join so I have both sets of data in one object but it runs terribly slow and I'm not hot enough to sort it out; I'd really appreciate a little guidance and explanation as to why it's so slow.

SELECT

`products`.`Product_number`,

`products`.`Price`,

`products`.`Previous_Price_1`,

`products`.`Previous_Price_2`,

`products`.`Product_number`,

`products`.`AverageOverallRating`,

`products`.`Name`,

`products`.`Brand_description`

FROM `product_categories`

LEFT OUTER JOIN `products`

ON `products`.`product_id`= `product_categories`.`product_id`

WHERE COALESCE(product_categories.cat4, product_categories.cat3,

product_categories.cat2, product_categories.cat1) = '123456'

AND `product_categories`.`product_id` != 0

The two tables are MyISAM, the products table has indexing on Product_number and Brand_Description and the product_categories table has a unique index on all columns combined; if this info is of any help at all.

Having inherited this system I need to get this working asap before I nuke it and do it properly so any help right now will earn you my utmost respect!

[Edit]

Here is the output of the explain extended:

+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+

| 1 | SIMPLE | product_categories | index | NULL | cat1 | 23 | NULL | 1224419 | 100.00 | Using where; Using index |

| 1 | SIMPLE | products | ALL | Product_id | NULL | NULL | NULL | 512376 | 100.00 | |

+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+

解决方案

The meaning of your query seems to be "find all products that have the category '123456'." Is that correct?

COALESCE is an extraordinarily expensive function to use in a WHERE statement, because it operates on index-hostile NULL values. Your explain result shows that your query is not being very selective on your product_categories table. In MySQL you need to avoid functions in WHERE statements altogether if you want to exploit indexes to make your queries fast.

The thing someone else said about 90-column tables being harmful is also true. But you're stuck with it, so let's just deal with it.

Can we rework your query to get rid of the function-based WHERE? Let's try this.

SELECT /* some columns from the products table */

FROM products

WHERE product_id IN

(

SELECT DISTINCT product_id

FROM product_categories

WHERE product_id <> 0

AND ( cat1='123456'

OR cat2='123456'

OR cat3='123456'

OR cat4='123456')

)

For this to work fast you're going to need to create separate indexes on your four cat columns. The composite unique index ("on all columns combined") is not going to help you. It still may not be so good.

A better solution might be FULLTEXT searching IN BOOLEAN MODE. You're working with the MyISAM access method so this is possible. It's definitely worth a try. It could be very fast indeed.

SELECT /* some columns from the products table */

FROM products

WHERE product_id IN

(

SELECT product_id

FROM product_categories

WHERE MATCH(cat1,cat2,cat3,cat4)

AGAINST('123456' IN BOOLEAN MODE)

AND product_id <> 0

)

For this to work fast you're going to need to create a FULLTEXT index like so.

CREATE FULLTEXT INDEX cat_lookup

ON product_categories (cat1, cat2, cat3, cat4)

Note that neither of these suggested queries produce precisely the same results as your COALESCE query. The way your COALESCE query is set up, some combinations won't match it that will match these queries. For example.

cat1 cat2 cat3 cat4

123451 123453 123455 123456 matches your and my queries

123456 123455 123454 123452 matches my queries but not yours

But it's likely that my queries will produce a useful list of products, even if it has a few more items in yours.

You can debug this stuff by just working with the inner queries on product_categories.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值