query optimization
am 22.09.2011 20:44:29 von supr_star--1529059597-868144110-1316717069=:93572
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
=A0I have a table with 24 million rows, I need to figure out how to o=
ptimize a query. =A0It has to do with mac addresses and radius packets - I =
want to see the # of connections and the min/max date. So I basically want =
all this data:
=A0 select cpe_mac,count(*) c,min(r3_dt) mindt,max(r3_d=
t) maxdt, max(rec_num) recn
=A0 from radiuscap
=A0 where r3_dt>=
=3DSUBDATE(NOW(),INTERVAL 30 DAY)
=A0 =A0 and r3_type=3D'Access'
=
=A0 group by cpe_mac order by cpe_mac=0A;
This piece of the query take=
s 30 seconds to run and produces 3500 rows. =A0I have r3_dt indexed. =A0I a=
lso want a status field of the row with the highest r3_dt:
select rec_=
num,cpe_mac,req_status
from rad_r3cap=0Awhere r3_type=3D'Access'
=
and (cpe_mac,r3_dt) in=A0(select cpe_mac,max(r3_dt) from rad_r3cap)=0A;=0A=
=0AThis piece of the query takes forever, I let it run for an hour and=
it still didn't finish, it's obviously not using indexes. =A0I have no ide=
a how far along it got. =A0I wrote a php script to run the 1st query, then =
do 3500 individual lookups for the status using the max(rec_num) field in t=
he 1st query, and I can get the data in 31 seconds. =A0So I CAN produce thi=
s data, but very slowly, and not in 1 sql query. =A0I want to consolidate t=
his into 1 sql so I can make a view.
If anyone can point me in the rig=
ht direction, I'd appreciate it!
mysql> desc rad_r3cap;=0A+-----=
--------+-------------+------+-----+---------+-------------- --+=0A| Field =
=A0 =A0 =A0 | Type =A0 =A0 =A0 =A0| Null | Key | Default | Extra =A0 =A0 =
=A0 =A0 =A0|=0A+-------------+-------------+------+-----+---------+- -------=
--------+=0A| rec_num =A0 =A0 | int(11) =A0 =A0 | NO =A0 | PRI | NULL =A0 =
=A0| auto_increment |=0A| r3_dt =A0 =A0 =A0 | datetime =A0 =A0| YES =A0| MU=
L | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| r3_micros =A0 | int(=
11) =A0 =A0 | YES =A0| =A0 =A0 | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0|=0A| r3_type =A0 =A0 | varchar(16) | YES =A0| =A0 =A0 | NULL =A0 =A0| =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| req_status =A0| varchar(16) | YES =A0|=
=A0 =A0 | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| req_comment |=
varchar(64) | YES =A0| =A0 =A0 | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0|=0A| asn_ip =A0 =A0 =A0| varchar(16) | YES =A0| MUL | NULL =A0 =A0| =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| asn_name =A0 =A0| varchar(16) | YES =
=A0| =A0 =A0 | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| bsid =A0 =
=A0 =A0 =A0| varchar(12) | YES =A0| MUL | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0|=0A| cpe_ip =A0 =A0 =A0| varchar(16) | YES =A0| =A0 =A0 | NULL=
=A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| cpe_mac =A0 =A0 | varchar(12=
) | YES =A0| MUL | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| filen=
ame =A0 =A0| varchar(32) | YES =A0| =A0 =A0 | NULL =A0 =A0| =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0|=0A| linenum =A0 =A0 | int(11) =A0 =A0 | YES =A0| =A0 =A0 =
| NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=0A| r3_hour =A0 =A0 | date=
time =A0 =A0| YES =A0| MUL | NULL =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|=
=0A| user_name =A0 | varchar(64) | YES =A0| =A0 =A0 | NULL =A0 =A0| =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0|=0A+-------------+-------------+------+-----+-----=
----+----------------+
mysql> show indexes in rad_r3cap;=0A+----------=
-+------------+--------------+--------------+-------------+- ----------+----=
---------+----------+--------+------+------------+---------+ =0A| Table =A0 =
=A0 | Non_unique | Key_name =A0 =A0 | Seq_in_index | Column_name | Collatio=
n | Cardinality | Sub_part | Packed | Null | Index_type | Comment |=0A+----=
-------+------------+--------------+--------------+--------- ----+----------=
-+-------------+----------+--------+------+------------+---- -----+=0A| rad_=
r3cap | =A0 =A0 =A0 =A0 =A00 | PRIMARY =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0=
1 | rec_num =A0 =A0 | A =A0 =A0 =A0 =A0 | =A0 =A023877677 | =A0 =A0 NULL | =
NULL =A0 | =A0 =A0 =A0| BTREE =A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap =
| =A0 =A0 =A0 =A0 =A00 | r3cap_dt =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A01 | r3_d=
t =A0 =A0 =A0 | A =A0 =A0 =A0 =A0 | =A0 =A0 =A0 =A0NULL | =A0 =A0 NULL | NU=
LL =A0 | YES =A0| BTREE =A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap | =A0 =
=A0 =A0 =A0 =A00 | r3cap_dt =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A02 | r3_micros =
=A0 | A =A0 =A0 =A0 =A0 | =A0 =A0 =A0 =A0NULL | =A0 =A0 NULL | NULL =A0 | Y=
ES =A0| BTREE =A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap | =A0 =A0 =A0 =
=A0 =A01 | r3cap_bsid =A0 | =A0 =A0 =A0 =A0 =A0 =A01 | bsid =A0 =A0 =A0 =A0=
| A =A0 =A0 =A0 =A0 | =A0 =A0 =A0 =A0 346 | =A0 =A0 NULL | NULL =A0 | YES =
=A0| BTREE =A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap | =A0 =A0 =A0 =A0 =
=A01 | r3cap_asnip =A0| =A0 =A0 =A0 =A0 =A0 =A01 | asn_ip =A0 =A0 =A0| A =
=A0 =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A055 | =A0 =A0 NULL | NULL =A0 | YES =A0=
| BTREE =A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap | =A0 =A0 =A0 =A0 =A01=
| r3cap_cpemac | =A0 =A0 =A0 =A0 =A0 =A01 | cpe_mac =A0 =A0 | A =A0 =A0 =
=A0 =A0 | =A0 =A0 =A0 =A04758 | =A0 =A0 NULL | NULL =A0 | YES =A0| BTREE =
=A0 =A0 =A0| =A0 =A0 =A0 =A0 |=0A| rad_r3cap | =A0 =A0 =A0 =A0 =A01 | r3cap=
_date =A0 | =A0 =A0 =A0 =A0 =A0 =A01 | r3_hour =A0 =A0 | A =A0 =A0 =A0 =A0 =
| =A0 =A0 =A0 =A01548 | =A0 =A0 NULL | NULL =A0 | YES =A0| BTREE =A0 =A0 =
=A0| =A0 =A0 =A0 =A0 |=0A+-----------+------------+--------------+---------=
-----+-------------+-----------+-------------+----------+--- -----+------+--=
----------+---------+=0A7 rows in set (0.00 sec)=0A
--1529059597-868144110-1316717069=:93572--