如何使用mysqli加快查询速度?

时间:2018-06-06 20:31:33

标签: mysqli

我有这个查询,需要117秒才能获得100000行,我可以在5秒或10秒内获得所有这些行吗?

 SELECT 
`nas`.`nasname`,
`nas`.`api_username`,
`nas`.`api_password`,
`nas`.`api_port`,
`nas`.`secret`,
`nas`.`up_by`,
COALESCE(`nas`.`api_is_enabled`,'0') as `api_is_en`,
`radacct`.`nasipaddress`,
`radcheck`.`address_list_name`,
`radcheck`.`address_val`,
`radacct`.`framedipaddress`,
`radacct`.`callingstationid`,
`radacct`.`radacctid` as `id_session`,
`radacct`.`framedprotocol`,
`radacct`.`username`,
`radacct`.`last_speed` ,
`radacct`.`acctsessionid` as `acctsessionid`,
`radip`.`value` as `ip_address`,
`radmac`.`value` as `mac_address`,
`radcount`.`value` as `simul_sess`,
COUNT(`radacct`.`radacctid`) as count_login ,
COALESCE(`card_users`.`id`,0) as `id_card`,
COALESCE(`card_users`.`download_qouta`,`userinfo`.`in_down`) *1024*1024 as `down_qouta`,
COALESCE(`card_users`.`upload_qouta`,`userinfo`.`in_up`) *1024*1024 as `up_qouta`,
COALESCE(`card_users`.`all_quota`,`userinfo`.`av_qouta`) *1024*1024 as `av_qouta`,
`card_users`.`date_end_card` as `date_end_card`,
`card_users`.`val_date` as `val_date`,
`card_users`.`per_second` as `per_second`,
`card_users`.`at_the_first_login` as `at_the_first_login`,
`card_users`.`exp_first_login` as `exp_first_login`,
`card_users`.`val_time_exp` as `val_time_exp`,
`card_users`.`time_cards_exp` as `time_cards_exp`,
`card_users`.`exp_quota` as `exp_quota`,
`card_users`.`exp_date` as `exp_date`,
`userinfo`.`updatedate` as `updatedate`,
`userinfo`.`divi_down_speed_slm` as `divi_down_speed_slm`,
`userinfo`.`divi_up_speed_slm` as `divi_up_speed_slm`,
`userinfo`.`arr_days` as `u_arr_days`,
`userinfo`.`value_choice` as `u_value_choice`,
`userinfo`.`last_end_day` as `last_end_day`,
`userinfo`.`macs` as `macs`,
 `p`.`profile_name` as `profile_name`,
`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,
`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,
`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,
`p`.`online_time` as `online_time`,
`p`.`hours_min` as `hours_min`,
`p`.`daily_online_time` as `daily_online_time`,
`p`.`daily_hours_min` as `daily_hours_min`,
`p`.`bandwidth_time` as `bandwidth_time`,
`p`.`daily_expire_service` as `daily_expire_service`,
`p`.`qouta_expire_service` as `qouta_expire_service`,
`p`.`profile_expire_service` as `profile_expire_service`,
`p`.`sp_up` as `sp_up`,
`p`.`sp_down` as `sp_down`,
`p`.`ch_day_end` as `ch_day_end`,
`p`.`set_day_end` as `set_day_end`,
`p`.`percent` as `percent`,
`p`.`arr_days` as `p_arr_days`,
`p`.`value_choice` as `p_value_choice`,
`r1`.`value` as `value_exp`,
`p1`.`bandwidth_time` as `band_exp`,
`p1`.`percent` as `percent_exp`,
`r2`.`value` as `val_address_exp_list`,
`r3`.`value` as `value_exp_daily`,
`p3`.`bandwidth_time` as `band_daily_exp`,
`p3`.`percent` as `percent_daily_exp`,
`r4`.`value` as `val_address_exp_daily_list`,
`r5`.`value` as `value_exp_serv`,
`p3`.`bandwidth_time` as `band_serv_exp`,
`p3`.`percent` as `percent_serv_exp`,
`r6`.`value` as `val_address_exp_serv_list`,
`r7`.`value` as `value_now`,
`r8`.`value` as `val_address_list`,
COALESCE(ROUND(time_to_sec(`userinfo`.`online_time`)),0) as `t_online_time` ,
COALESCE(ROUND(time_to_sec(`userinfo`.`daily_online_time`)),0) as `d_online_time`,
UNIX_TIMESTAMP(STR_TO_DATE(`radexp`.`value`, '%d %b %Y %H:%i')) as `exp_user`,
`radacct`.`callingstationid` as  `callingstationid`,
UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) as `session_st_date`
FROM `radacct`
INNER JOIN `radcheck` 
    ON `radacct`.`username` = `radcheck`.`username` 
   AND `radcheck`.`attribute` = 'Cleartext-Password' 
INNER JOIN `radusergroup` `r9` 
    ON `r9`.`username` = `radcheck`.`username`
LEFT JOIN `userinfo` 
    ON `userinfo`.`username` = `r9`.`username` 
LEFT JOIN (
    SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`
         , SUM(`radacct`.`acctoutputoctets`) as `down_today`
         , SUM(`radacct`.`acctsessiontime`) as `daily_time`
         , `radacct`.`username` 
    FROM `radacct` 
    WHERE  DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') 
    GROUP BY `username`
) as `rad2` 
    ON `rad2`.`username` = `r9`.`username` 
INNER JOIN `nas` 
    ON `nas`.`nasname`=`radacct`.`nasipaddress` 
LEFT JOIN `profiles` `p` 
    ON `p`.`profile_name` = `r9`.`groupname`
LEFT JOIN `profiles` `p1` 
    ON `p1`.`id` = `p`.`qouta_expire_service`
LEFT JOIN `profiles` `p2` 
    ON `p2`.`id` = `p`.`daily_expire_service`
LEFT JOIN `profiles` `p3` 
    ON `p3`.`id` = `p`.`profile_expire_service`
LEFT JOIN `radgroupreply` `r1` 
    ON `r1`.`groupname` = `p1`.`profile_name` 
   AND `r1`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r2` 
    ON `r2`.`groupname` = `p1`.`profile_name` 
   AND `r2`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r3` 
    ON `r3`.`groupname` = `p2`.`profile_name` 
   AND `r3`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r4` 
    ON `r4`.`groupname` = `p2`.`profile_name` 
   AND `r4`.`attribute` = 'Mikrotik-Address-List' 
LEFT JOIN `radgroupreply` `r5` 
    ON `r5`.`groupname` = `p3`.`profile_name` 
   AND `r5`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r6` 
    ON `r6`.`groupname` = `p3`.`profile_name` 
   AND `r6`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `radgroupreply` `r7` 
    ON `r7`.`groupname` = `p`.`profile_name` 
   AND `r7`.`attribute` = 'Mikrotik-Rate-Limit'
LEFT JOIN `radgroupreply` `r8` 
    ON `r8`.`groupname` = `p`.`profile_name` 
   AND `r8`.`attribute` = 'Mikrotik-Address-List'
LEFT JOIN `card_users` 
    ON `card_users`.`id` = `radcheck`.`id_card`
LEFT JOIN `radreply` as `radip` 
    ON `radip`.`username` = `r9`.`username` 
   AND `radip`.`attribute`='Framed-IP-Address'
LEFT JOIN `radcheck` as `radmac` 
    ON `radmac`.`username` = `r9`.`username` 
   AND `radmac`.`attribute`='Calling-Station-Id'
LEFT JOIN `radcheck` as `radcount` 
    ON `radcount`.`username` = `r9`.`username` 
   AND `radcount`.`attribute`='Simultaneous-Use'
LEFT JOIN `radcheck` as `radexp` 
    ON `radexp`.`username` = `r9`.`username` 
   AND `radexp`.`attribute`='Expiration'
WHERE ( radacct .AcctStopTime IS NULL 
    OR radacct.AcctStopTime = '0000-00-00 00:00:00')
GROUP BY `radacct`.`username`

explain this query

从查询中删除此内容时,速度提高60%

LEFT JOIN (SELECT SUM(`radacct`.`acctinputoctets`) as `up_today`, 
SUM(`radacct`.`acctoutputoctets`) as `down_today`,
SUM(`radacct`.`acctsessiontime`) as `daily_time`,
`radacct`.`username` FROM `radacct` 
WHERE  DATE_FORMAT(STR_TO_DATE(`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') 
GROUP BY `username`) as `rad2` ON (`rad2`.`username` = `r9`.`username`) 

我有所有这些表的索引,那么我的问题是什么?

注意:我有电脑 cpu core i 5 ram 4千兆

编辑:请帮我解决这个问题

此图片用于解释我的表格

0 个答案:

没有答案