If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Can this query can be faster?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-28-11, 03:17
s197oo302 s197oo302 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
Can this query can be faster?

I had two table and joined query took some long time.
I added indexes but still it is slow (about 14 seconds).
Can you tell me what can be modified?
it took about 10 seconds, too late still.

My table
mysql> desc CDS_SERVER_LOG_TBL;
+---------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+-------+
| RequestTime | varchar(12) | YES | | NULL | |
| DirectionGubun | varchar(2) | YES | | NULL | |
| Min | varchar(12) | YES | MUL | NULL | |
| NewMin | varchar(12) | YES | | NULL | |
| JuminNum | varchar(13) | YES | | NULL | |
| ServiceAdminNum | varchar(10) | YES | | NULL | |
| Mdn | varchar(12) | YES | | NULL | |
| Imsi | varchar(15) | YES | | NULL | |
| KIT | varchar(50) | YES | | NULL | |
| EquipType | varchar(4) | YES | | NULL | |
| OptionalServiceCode | varchar(30) | YES | MUL | NULL | |
| CDS_LOG_DATE | datetime | YES | MUL | NULL | |
+---------------------+-------------+------+-----+---------+-------+
mysql> desc OPTIONAL_SERVICE_CODE_TB
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| CDSBitLocation | varchar(2) | YES | MUL | NULL | |
| OptionalServCode | varchar(10) | NO | | | |
| TIGServCode | varchar(15) | YES | | NULL | |
| ServiceName | varchar(100) | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+
My index
create index cds_idx1 on CDS_SERVER_LOG_TBL (CDS_LOG_DATE, DirectionGubun) USING BTREE;
create index cds_idx2 on CDS_SERVER_LOG_TBL (OptionalServiceCode) USING BTREE;
create index cds_idx3 on CDS_SERVER_LOG_TBL (MIN) USING BTREE;
create index optional_idx1 on OPTIONAL_SERVICE_CODE_TBL(CDSBitLocation) USING BTREE;

my query plan is
EXPLAIN SELECT
COUNT(MIN)
FROM CDS_SERVER_LOG_TBL AS a, OPTIONAL_SERVICE_CODE_TBL AS b
WHERE a.OptionalServiceCode=b.CDSBitLocation
AND DATE_FORMAT(a.CDS_LOG_DATE,'%Y-%m-%d') BETWEEN DATE_FORMAT('20111109','%Y-%m-%d') AND DATE_FORMAT('20111110','%Y-%m-%d')
AND (a.DirectionGubun = 'A1' OR a.DirectionGubun = 'Z2' OR a.DirectionGubun = 'C1')
;

result it took about 10 seconds
+----+-------------+-------+------+---------------+---------------+---------+----------------------------------------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+---------------+---------+----------------------------------------+---------+--------------------------+
| 1 | SIMPLE | a | ALL | cds_idx2 | NULL | NULL | NULL | 6152116 | Using where |
| 1 | SIMPLE | b | ref | optional_idx1 | optional_idx1 | 7 | customer_service.a.OptionalServiceCode | 3 | Using where; Using index |
+----+-------------+-------+------+---------------+---------------+---------+----------------------------------------+---------+--------------------------+
Reply With Quote
  #2 (permalink)  
Old 11-28-11, 08:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by s197oo302 View Post
Can you tell me what can be modified?
here's your problem --
Code:
AND DATE_FORMAT(a.CDS_LOG_DATE,'%Y-%m-%d') BETWEEN ...
this requires a table scan

change it to this --
Code:
AND a.CDS_LOG_DATE BETWEEN ...
obviously, you may wish to change the "between" values
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-28-11, 14:46
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Also change:
Code:
AND (a.DirectionGubun = 'A1' OR a.DirectionGubun = 'Z2' OR a.DirectionGubun = 'C1')
to:
Code:
AND a.DirectionGubun IN ( 'A1','Z2','C1')
Dave Nance
Reply With Quote
  #4 (permalink)  
Old 11-28-11, 14:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
good call, dave, that's a lot better for readability and simplicity

but, just so that nobody gets the wrong impression (see thread title), this improvement actually does not have any effect on performance
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-28-11, 20:37
s197oo302 s197oo302 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
first modification and seconds modification

First "AND a.CDS_LOG_DATE BETWEEN ... "modification truly took effect and use index and below 1 seconds result.
Thank you, I didn't recognize that point before, but I am not sure

Seconds modification make change or getting wrong effect.
after change "AND a.DirectionGubun IN ( 'A1','Z2','C1'),"
It took about 0.02 seconds more.

So I like to use query based on modification of CDS_LOG_DATE.
Thank you very much for you help.

Oracle I am accustomed little bit more, but mysql query still difficult to me.
but basis is almost same.
Reply With Quote
Reply

Tags
index, sql, tuning

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On