简体   繁体   English

MySQL性能调优

[英]Mysql performance tuning

I'm trying to figure out how i can improve the performance of this query and i believe it may be my indexes; 我试图弄清楚如何提高此查询的性能,并且我相信这可能是我的索引; some of my thoughts are that date may be causing the poor performance or that i have the indexs in the order wrong. 我的一些想法是日期可能导致性能不佳,或者索引顺序错误。 Also are there any other suggestions anyone has on how to improve the speed that are not index related? 还有没有其他人建议如何提高与索引无关的速度? Thanks, i look forward to any input! 谢谢,我期待任何输入!

Here's what I've tried so far 到目前为止,这是我尝试过的

 ALTER TABLE data ADD INDEX(data_timestamp, first,last);
 ALTER table data add index(first);
 ALTER table data add index(first);
 ALTER TABLE data add index (data_timestamp);

The following query(the second one) below runs a subquery for each row of a database in order to get the previous average at the instant of each point 下面的查询(第二个查询)对数据库的每一行运行一个子查询,以便在每个点的瞬间获得先前的平均值

select count(*) from data where data_timestamp like '2015-01-01%'; -> 362855

select (select sum(first*last) / sum(last)
FROM data t2
WHERE data_timestamp like '2015-12-18%'
AND t2.data_timestamp <= t1.data_timestamp
), t1.*
FROM data t1
WHERE data_timestamp like '2015-12-18%';

For optimum performance, you want an index range scan operation for the data_timestamp column. 为了获得最佳性能,您需要对data_timestamp列进行索引范围扫描操作。 The predicate in the query of the form: 查询形式中的谓词:

  WHERE data_timestamp LIKE '2015-12-18%' 

is forcing MySQL to evaluate EVERY value of data_timestamp in the table, effectively converting the datetime/timestamp value into a string, and then performing a string comparison on the converted value. 是强制MySQL评估表中data_timestamp每个值,有效地将datetime / timestamp值转换为字符串,然后对转换后的值执行字符串比较。

If we use a predicate with a comparison to datetime values, then MySQL can make more effective use of an index that has data_timestamp as a leading column. 如果我们使用与日期时间值进行比较的谓词,则MySQL可以更有效地利用以data_timestamp作为前导列的索引。 For example: 例如:

  WHERE data_timestamp >= '2015-12-18'
    AND data_timestamp <  '2015-12-18' + INTERVAL 1 DAY

The EXPLAIN output for a query using the LIKE pattern will show 使用LIKE模式的查询的EXPLAIN输出将显示

type
------
index

That shows the query can make use of an index. 这表明查询可以利用索引。 But it's doing a full scan of the index, looking at every row in the index. 但是它正在对索引进行全面扫描,查看索引中的每一行。 But a much more efficient pattern is available. 但是可以使用更有效的模式。 We can allow MySQL to quickly eliminate vast swaths of rows in the index from being considered, by using a range scan operation. 我们可以允许MySQL通过使用范围扫描操作来快速消除索引中的大量行。 A query with a predicate as in the second example will (should) show: 如第二个示例中带有谓词的查询将(应该)显示:

type
------
range

That's going to improve performance for a query that is pulling a relatively small number of rows from a large set. 这将提高从大集合中提取相对较少数量的行的查询的性能。


More explanation, in case I didn't make this clear. 更多说明,以防万一我不清楚。 Writing: 写作:

  WHERE ts_col LIKE '2015-12-18%' 

is effectively the same as writing 实际上与写作相同

  WHERE CONVERT(ts_col,CHAR(18)) LIKE '2015-12-18%' 

And that forces MySQL to perform the CONVERT operation on the value in the ts_col for every row in the table. 并且这迫使MySQL对ts_col中的每一行的值执行CONVERT操作。

BOTTOM LINE 底线

Don't force unnecessary datatype conversions of columns from the table. 不要强制表中列的不必要的数据类型转换。 Instead, compare columns to their native datatypes. 而是将列与其本机数据类型进行比较。

You can do an "explain" query to the database to figure out what is happening. 您可以对数据库进行“解释”查询,以了解正在发生的情况。 Just write "explain" before any query. 只需在任何查询之前写“说明”即可。

Is not necessary to add indexes for first and last. 不必为第一个和最后一个添加索引。 You're only searching in data_timestamp fields so that's the only one index you need. 您仅在data_timestamp字段中进行搜索,因此这是您唯一需要的一个索引。

On the other hand, you might be having problems regarding to using "like %" in date columns. 另一方面,在日期列中使用“ like%”可能会遇到问题。 Check if there are another alternatives to do the same. 检查是否还有其他替代方法可以执行此操作。 If data_timestamp is a text column, you should add a full text index to the field. 如果data_timestamp是文本列,则应向该字段添加全文本索引。 If data_timestamp is a date column, use "between" instead "like". 如果data_timestamp是日期列,请使用“ between”代替“ like”。 "explain" tells you which index is used by the query. “解释”告诉您查询使用哪个索引。

For this query you need only ALTER TABLE tic_data add index (data_timestamp) . 对于此查询,您仅需要ALTER TABLE tic_data add index (data_timestamp) But data_timestamp must be of CHAR or VARCHAR type (you scan it with LIKE <string%> ). 但是data_timestamp必须为CHARVARCHAR类型(使用LIKE <string%>扫描)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM