简体   繁体   English

优化查询性能

[英]Optimize query performance

My Appliation(using php) performance is very slow due to more data in my mysql table, My table Structure as follows, 由于我的mysql表中的数据更多,我的Appliation(使用php)性能非常慢,我的表结构如下,

`CREATE TABLE `xxxxx` 
(`ID` int(11) NOT NULL AUTO_INCREMENT,
 `IP` varchar(45) NOT NULL,
 `Cname` varchar(45) NOT NULL,
 `Port` int(5) NOT NULL DEFAULT'3306',
 `Variable` varchar(150) DEFAULT NULL,
 `status` varchar(100) DEFAULT NULL,
 `moddate` datetime NOT NULL,
 `Servertime` datetime NOT NULL,
 PRIMARY KEY (`ID`,`moddate`),
 KEY `idx_variable` (`Variable`) ) 
  ENGINE=InnoDB AUTO_INCREMENT=85054928 DEFAULT CHARSET=ucs2
    /*!50100 PARTITION BY LIST ( MONTH (moddate))
    (PARTITION Jan VALUES IN (1) ENGINE = InnoDB,
    PARTITION Feb VALUES IN (2) ENGINE = InnoDB,
    PARTITION Mar VALUES IN (3) ENGINE = InnoDB,
    PARTITION Apr VALUES IN (4) ENGINE = InnoDB,
    PARTITION May VALUES IN (5) ENGINE = InnoDB,
    PARTITION Jun VALUES IN (6) ENGINE = InnoDB,
    PARTITION Jul VALUES IN (7) ENGINE = InnoDB,
    PARTITION Aug VALUES IN (8) ENGINE = InnoDB,
    PARTITION Sep VALUES IN (9) ENGINE = InnoDB,
    PARTITION Oct VALUES IN (10) ENGINE = InnoDB,
    PARTITION Nov VALUES IN (11) ENGINE = InnoDB,
    PARTITION DECM VALUES IN (12) ENGINE = InnoDB) */`

And It has around 4.2 GB data. 并且它具有约4.2 GB的数据。 My select statement for fetch data is, 我的用于提取数据的select语句是,

SELECT 
      `status`,
      `moddate` 
   FROM 
      `xxxxxx` 
   WHERE 
          Cname='xxxx' 
      and ip='0.0.0.0' 
      and port='3306' 
      and Variable='xxxxxx' 
      and status REGEXP '^[0-9]+$|^ON$' 
      and moddate between '2015-01-01 00:47' and '2015-02-01 01:07';

but it takes 4:00 min. 但是需要4:00分钟 Since 15 days i have been suffering by this problem. 自15天以来,我一直在遭受这个问题的困扰。 I know we need to put index on column. 我知道我们需要将索引放在列上。 But I don't know how to optimize this. 但是我不知道如何优化它。 Could you please any one suggest me which field needs to indexed. 能否请任何一个建议我建议哪个字段需要索引。 or share any other link to optimize my application. 或共享任何其他链接来优化我的应用程序。 thanx... 谢谢...

I would look to the where clause and try to determine what might be the smaller result set field first and have that in the first position of a composite index 我将查看where子句,并尝试首先确定较小的结果集字段,然后将其放在复合索引的第一位置

You may have many entries for the client "xxxxx", but a smaller set on the IP address. 客户端“ xxxxx”的条目可能很多,但IP地址上的条目却较小。 Not sure what your "Variable" context is, maybe this is smaller. 不知道您的“可变”上下文是什么,也许这会更小。 But I would try on 但我会尝试

( IP, port, cname, variable, moddate, status )

but that is basically an index on ALMOST everything. 但这基本上是所有内容的索引。 So, that said, I might chop it down to see performance based on... 所以,也就是说,我可能会降低它以查看基于...的性能

( IP, port, cname, moddate )

After thinking more on it, having CName in first position is probably better... 经过更多思考之后,将CName放在第一位可能会更好。

( CName, IP, port, variable, moddate, status )

as it hit me cname is probably like a website address, and since a server on a single IP can host many website names, the CName would be smaller than the overall IP address. 因为它很常见,所以cname可能就像一个网站地址,并且由于单个IP上的服务器可以托管许多网站名称,因此CName会小于整个IP地址。

This is your query: 这是您的查询:

SELECT status, moddate
FROM xxxxx 
WHERE Cname = 'xxxxxx' and
      ip = '0.0.0.0' and 
      port = 3306 and
      Variable = 'TABLE_LOCKS_IMMEDIATE' and
      status REGEXP '^[0-9]+$|^ON$' and
      moddate between '2015-01-01 00:47' and '2015-02-01 01:07';

Note: port is an integer so I removed the single quotes around 3306 . 注意: port是一个整数,因此我删除了3306附近的单引号。 Sometimes, in some databases, the use of mixed comparisons can affect the use of indexes (although probably not in this case). 有时,在某些数据库中,使用混合比较会影响索引的使用(尽管在这种情况下可能不会)。

Your query is accessing one table, so the place to start with defining an index is the WHERE clause. 您的查询正在访问一个表,因此从定义索引开始的地方是WHERE子句。 An index that has all six columns mentioned in the table is called a "covering" index. 具有表中提到的所有六列的索引称为“覆盖”索引。 And a covering index is usually the best for performance. 覆盖指数通常是性能最好的。

For such an index, start with the columns that have equality conditions. 对于这样的索引,请从具有相等条件的列开始。 Then you get one position for inequalities and in . 然后,对于不等式和in您将获得一个位置。 Then the rest of the columns are in the index just so the engine doesn't have to access the data pages. 然后,其余的列都在索引中,这样引擎就不必访问数据页了。 So, the best index is xxxxxx(Cname, ip, port, Variable, moddate, status) . 因此,最佳索引是xxxxxx(Cname, ip, port, Variable, moddate, status) The ordering of the first four columns does not make a different for this query, because all have equality indexes in the from clause. 前四列的顺序对此查询没有影响,因为所有列在from子句中都有相等的索引。

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

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