Mysql索引COLLATE utf8_unicode_ci

时间:2015-07-01 19:31:40

标签: mysql indexing collate

我从freeradius更新了数据库,现在有一个sql语句需要花费30秒才能执行,之前只执行0.5秒。

这是OLD定义表:

    CREATE TABLE `radacct` (
   `radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
   `acctsessionid` varchar(64) NOT NULL DEFAULT '',
   `acctuniqueid` varchar(32) NOT NULL DEFAULT '',
   `username` varchar(64) NOT NULL DEFAULT '',
   `groupname` varchar(64) NOT NULL DEFAULT '',
   `realm` varchar(64) DEFAULT '',
   `nasipaddress` varchar(15) NOT NULL DEFAULT '',
   `nasportid` varchar(15) DEFAULT NULL,
   `nasporttype` varchar(32) DEFAULT NULL,
   `acctstarttime` datetime DEFAULT NULL,
   `acctstoptime` datetime DEFAULT NULL,
   `acctsessiontime` int(12) DEFAULT NULL,
   `acctauthentic` varchar(32) DEFAULT NULL,
   `connectinfo_start` varchar(50) DEFAULT NULL,
   `connectinfo_stop` varchar(50) DEFAULT NULL,
   `acctinputoctets` bigint(20) DEFAULT NULL,
   `acctoutputoctets` bigint(20) DEFAULT NULL,
   `calledstationid` varchar(50) NOT NULL DEFAULT '',
   `callingstationid` varchar(50) NOT NULL DEFAULT '',
   `acctterminatecause` varchar(32) NOT NULL DEFAULT '',
   `servicetype` varchar(32) DEFAULT NULL,
   `framedprotocol` varchar(32) DEFAULT NULL,
   `framedipaddress` varchar(15) NOT NULL DEFAULT '',
   `acctstartdelay` int(12) DEFAULT NULL,
   `acctstopdelay` int(12) DEFAULT NULL,
   `xascendsessionsvrkey` varchar(10) DEFAULT NULL,
   PRIMARY KEY (`radacctid`),
   KEY `username` (`username`),
   KEY `framedipaddress` (`framedipaddress`),
   KEY `acctsessionid` (`acctsessionid`),
   KEY `acctsessiontime` (`acctsessiontime`),
   KEY `acctuniqueid` (`acctuniqueid`),
   KEY `acctstarttime` (`acctstarttime`),
   KEY `acctstoptime` (`acctstoptime`),
   KEY `nasipaddress` (`nasipaddress`)
 ) ENGINE=InnoDB AUTO_INCREMENT=3514770 DEFAULT CHARSET=latin1;

 CREATE TABLE `userinfo` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `username` varchar(128) DEFAULT NULL,
   `firstname` varchar(200) DEFAULT NULL,
   `lastname` varchar(200) DEFAULT NULL,
   `email` varchar(200) DEFAULT NULL,
   `department` varchar(200) DEFAULT NULL,
   `company` varchar(200) DEFAULT NULL,
   `workphone` varchar(200) DEFAULT NULL,
   `homephone` varchar(200) DEFAULT NULL,
   `mobilephone` varchar(200) DEFAULT NULL,
   `address` varchar(200) DEFAULT NULL,
   `city` varchar(200) DEFAULT NULL,
   `state` varchar(200) DEFAULT NULL,
   `country` varchar(100) DEFAULT NULL,
   `zip` varchar(200) DEFAULT NULL,
   `notes` varchar(200) DEFAULT NULL,
   `changeuserinfo` varchar(128) DEFAULT NULL,
   `portalloginpassword` varchar(128) DEFAULT '',
   `enableportallogin` int(32) DEFAULT '0',
   `creationdate` datetime DEFAULT '0000-00-00 00:00:00',
   `creationby` varchar(128) DEFAULT NULL,
   `updatedate` datetime DEFAULT '0000-00-00 00:00:00',
   `updateby` varchar(128) DEFAULT NULL,
   PRIMARY KEY (`id`),
   KEY `username` (`username`),
   KEY `company` (`company`)
 ) ENGINE=MyISAM AUTO_INCREMENT=188493 DEFAULT CHARSET=latin1;

这是新的定义表:

 CREATE TABLE `radacct` (
   `radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
   `acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
   `nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
   `nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `acctstarttime` datetime DEFAULT NULL,
   `acctstoptime` datetime DEFAULT NULL,
   `acctsessiontime` int(12) DEFAULT NULL,
   `acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
   `connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
   `acctinputoctets` bigint(20) DEFAULT NULL,
   `acctoutputoctets` bigint(20) DEFAULT NULL,
   `calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctstartdelay` int(12) DEFAULT NULL,
   `acctstopdelay` int(12) DEFAULT NULL,
   `xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
   PRIMARY KEY (`radacctid`),
   KEY `username` (`username`),
   KEY `framedipaddress` (`framedipaddress`),
   KEY `acctsessionid` (`acctsessionid`),
   KEY `acctsessiontime` (`acctsessiontime`),
   KEY `acctuniqueid` (`acctuniqueid`),
   KEY `acctstarttime` (`acctstarttime`),
   KEY `acctstoptime` (`acctstoptime`),
   KEY `nasipaddress` (`nasipaddress`)
 ) ENGINE=MyISAM AUTO_INCREMENT=3519495 DEFAULT CHARSET=utf8    COLLATE=utf8_unicode_ci;

 CREATE TABLE `userinfo` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `username` varchar(128) DEFAULT NULL,
   `firstname` varchar(200) DEFAULT NULL,
   `lastname` varchar(200) DEFAULT NULL,
   `email` varchar(200) DEFAULT NULL,
   `department` varchar(200) DEFAULT NULL,
   `company` varchar(200) DEFAULT NULL,
   `workphone` varchar(200) DEFAULT NULL,
   `homephone` varchar(200) DEFAULT NULL,
   `mobilephone` varchar(200) DEFAULT NULL,
   `address` varchar(200) DEFAULT NULL,
   `city` varchar(200) DEFAULT NULL,
   `state` varchar(200) DEFAULT NULL,
   `country` varchar(100) DEFAULT NULL,
   `zip` varchar(200) DEFAULT NULL,
   `notes` varchar(200) DEFAULT NULL,
   `changeuserinfo` varchar(128) DEFAULT NULL,
   `portalloginpassword` varchar(128) DEFAULT '',
   `enableportallogin` int(32) DEFAULT '0',
   `creationdate` datetime DEFAULT '0000-00-00 00:00:00',
   `creationby` varchar(128) DEFAULT NULL,
   `updatedate` datetime DEFAULT '0000-00-00 00:00:00',
   `updateby` varchar(128) DEFAULT NULL,
   PRIMARY KEY (`id`),
   KEY `username` (`username`),
   KEY `company` (`company`)
 ) ENGINE=MyISAM AUTO_INCREMENT=188894 DEFAULT CHARSET=latin1;

注意,OLD表同时具有CHARSET = latin1,而NEW表按定义具有不同的字符集和整理:CHARSET = utf8 COLLATE = utf8_unicode_ci和CHARSET = latin1。这应该是不同的。

现在这是我跑的句子:

 select TIMESTAMPDIFF(SECOND,max(acctstarttime),now()) as segons,(department) as idClient,(userinfo.username)
        from
        (select radacct.username as id
            from radacct,userinfo
            where userinfo.company=98
            and radacct.username = userinfo.username 
            group by userinfo.username
            order by max(radacct.radacctid) desc limit 0,6) as tbl,radacct,userinfo
        where radacct.username=tbl.id and radacct.username = userinfo.username 
        group by radacct.username
        order by max(radacct.radacctid) desc;

这是OLD TABLES的DESCRIBE - FAST查询(0.4s):

   id * select_type *   table   type    possible_keys   key key_len ref rows    Extra *
   1    PRIMARY <derived2>  ALL {null}  {null}  {null}  {null}  6   Using temporary; Using filesort
   1    PRIMARY radacct ref username    username    66  tbl.id  5   
   1    PRIMARY userinfo    ref username    username    131 radius.radacct.username 1   Using where
   2    DERIVED radacct index   username    username    66  {null}  28768   Using index; Using temporary; Using filesort
   2    DERIVED userinfo    ref username,company    username    131 radius.radacct.username 1   Using where

这是NEW TABLES - SLOW查询(30s)的DESCRIBE:

   id * select_type *   table   type    possible_keys   key key_len ref rows    Extra *
   1    PRIMARY <derived2>  ALL {null}  {null}  {null}  {null}  6   Using temporary; Using filesort
   1    PRIMARY radacct ref username    username    194 tbl.id  11  
   1    PRIMARY userinfo    ALL {null}  {null}  {null}  {null}  188911  Using where; Using join buffer
   2    DERIVED userinfo    ALL company {null}  {null}  {null}  188911  Using where; Using temporary; Using filesort
   2    DERIVED radacct ref username    username    194 func    11  Using where

感谢。

编辑:

我在userinfo上更改了charset:

 alter table radius.userinfo convert to character set utf8 collate utf8_unicode_ci;

现在新的 - 慢速查询在3秒内运行,但仍然没有OLD数据库那么快。

另外,我在两个查询中将VARCHAR长度更改为相同,但仍然不像OLD数据库那样按索引进行。

  ALTER TABLE radius.userinfo CHANGE username username VARCHAR(64);

我只是运行缓慢的子查询:

 select radacct.username as id
        from radacct,userinfo
        where userinfo.company=98
        and radacct.username = userinfo.username 
        group by userinfo.username
        order by max(radacct.radacctid) desc limit 0,6

这是DESCRIBE OLD - 快速:

 id select_type table   type    possible_keys   key key_len ref rows    Extra
 1  SIMPLE  radacct index   username    username    66      37545   Using index; Using temporary; Using filesort
 1  SIMPLE  userinfo    ref username,company    username    131 radius.radacct.username 1   Using where

这是DESCRIBE NEW - 慢:

 id select_type table   type    possible_keys   key key_len ref rows    Extra
 1  SIMPLE  radacct ALL username                56879   Using temporary; Using filesort
 1  SIMPLE  userinfo    ref company,username    username    195 radius.radacct.username 1   Using where  

为什么没有获得radacct.username索引?

编辑2:使用新的COLLATION-CHARSET添加新定义。

 CREATE TABLE `radacct` (
   `radacctid` bigint(21) NOT NULL AUTO_INCREMENT,
   `acctsessionid` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctuniqueid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `username` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `groupname` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `realm` varchar(64) COLLATE utf8_unicode_ci DEFAULT '',
   `nasipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `nasportid` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
   `nasporttype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `acctstarttime` datetime DEFAULT NULL,
   `acctstoptime` datetime DEFAULT NULL,
   `acctsessiontime` int(12) DEFAULT NULL,
   `acctauthentic` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `connectinfo_start` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
   `connectinfo_stop` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
   `acctinputoctets` bigint(20) DEFAULT NULL,
   `acctoutputoctets` bigint(20) DEFAULT NULL,
   `calledstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `callingstationid` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctterminatecause` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `servicetype` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `framedprotocol` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
   `framedipaddress` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
   `acctstartdelay` int(12) DEFAULT NULL,
   `acctstopdelay` int(12) DEFAULT NULL,
   `xascendsessionsvrkey` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
   PRIMARY KEY (`radacctid`),
   KEY `framedipaddress` (`framedipaddress`),
   KEY `acctsessionid` (`acctsessionid`),
   KEY `acctsessiontime` (`acctsessiontime`),
   KEY `acctuniqueid` (`acctuniqueid`),
   KEY `acctstarttime` (`acctstarttime`),
   KEY `acctstoptime` (`acctstoptime`),
   KEY `nasipaddress` (`nasipaddress`),
   KEY `username` (`username`)
 ) ENGINE=MyISAM AUTO_INCREMENT=3607301 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 CREATE TABLE `userinfo` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `username` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
   `firstname` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `lastname` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `email` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `department` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `company` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `workphone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `homephone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `mobilephone` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `address` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `city` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `state` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `country` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
   `zip` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `notes` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
   `changeuserinfo` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
   `portalloginpassword` varchar(128) COLLATE utf8_unicode_ci DEFAULT '',
   `enableportallogin` int(32) DEFAULT '0',
   `creationdate` datetime DEFAULT '0000-00-00 00:00:00',
   `creationby` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
   `updatedate` datetime DEFAULT '0000-00-00 00:00:00',
   `updateby` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
   PRIMARY KEY (`id`),
   KEY `company` (`company`),
   KEY `username` (`username`)
 ) ENGINE=MyISAM AUTO_INCREMENT=191546 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;     

1 个答案:

答案 0 :(得分:0)

我看到3个问题:

company = 98
`company` varchar(200) DEFAULT NULL,

如果是数字,则使用数值数据类型。

替换

KEY `company` (`company`)

INDEX(company, username)

但也许真正的问题是:

radacct.username = userinfo.username

一个是latin1而另一个是utf8。