473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bad performance caused by DISTINCT?

This is the query:
SELECT DAY(date_time) "day", COUNT( DISTINCT session_id ) "count"
FROM application_requests
WHERE (retailer_id = '111111-1111-11111')
AND(year_for_reports = 2006)
AND(month_for_reports = 9)
GROUP BY DAY( date_time );

EXPLAIN returns this:
"1" | "SIMPLE" | "application_requests" | "ref" | "index_for_reports" |
"index_for_reports" | "53" | "const,const,const" | "998840" | "Using
where; Using filesort"

index_for_reports is (retailer_id, year_for_reports, month_for_reports)

Table structure goes as follow:
id(int)
retailer_id(varchar[40])
date_time(datetime)
session_id(varchar[255])
controller(varchar[255])
action(varchar[255])
parameters(text)
product_line_id(int)
merchandise_id(int)
month_for_reports(int)
year_for_reports(int)

Any idea how I could make this faster? Right now I'm clocking at 555703
ms, which is intolerable.

Nov 13 '06 #1
3 3368

Heist wrote:
This is the query:
SELECT DAY(date_time) "day", COUNT( DISTINCT session_id ) "count"
FROM application_requests
WHERE (retailer_id = '111111-1111-11111')
AND(year_for_reports = 2006)
AND(month_for_reports = 9)
GROUP BY DAY( date_time );

EXPLAIN returns this:
"1" | "SIMPLE" | "application_requests" | "ref" | "index_for_reports" |
"index_for_reports" | "53" | "const,const,const" | "998840" | "Using
where; Using filesort"

index_for_reports is (retailer_id, year_for_reports, month_for_reports)

Table structure goes as follow:
id(int)
retailer_id(varchar[40])
date_time(datetime)
session_id(varchar[255])
controller(varchar[255])
action(varchar[255])
parameters(text)
product_line_id(int)
merchandise_id(int)
month_for_reports(int)
year_for_reports(int)

Any idea how I could make this faster? Right now I'm clocking at 555703
ms, which is intolerable.
A couple of things spring to mind, although i don't know what, if
anything, any of these will actually do for performance:

Could retailer ID be stored as an integer? if you needed the hyphens
for display purposes you could use something like this:

SELECT INSERT (

INSERT (
retailer_id,
7,
0,
'-'
),
12,
0,
'-'
)

varchar 255 for session_id sounds very large. Could it be smaller?

month_for_reports is only ever going to be 1-12 so it might as well be
tinyint

other than that, make sure everything is properly indexed - but I guess
you've done that already?
HIH

Nov 14 '06 #2
retailer_id cannot be stored as an integer sadly, but session_id could
be smaller. And yes, indexes are seemingly optimal. Thanks for the
answer.

strawberry wrote:
Heist wrote:
This is the query:
SELECT DAY(date_time) "day", COUNT( DISTINCT session_id ) "count"
FROM application_requests
WHERE (retailer_id = '111111-1111-11111')
AND(year_for_reports = 2006)
AND(month_for_reports = 9)
GROUP BY DAY( date_time );

EXPLAIN returns this:
"1" | "SIMPLE" | "application_requests" | "ref" | "index_for_reports" |
"index_for_reports" | "53" | "const,const,const" | "998840" | "Using
where; Using filesort"

index_for_reports is (retailer_id, year_for_reports, month_for_reports)

Table structure goes as follow:
id(int)
retailer_id(varchar[40])
date_time(datetime)
session_id(varchar[255])
controller(varchar[255])
action(varchar[255])
parameters(text)
product_line_id(int)
merchandise_id(int)
month_for_reports(int)
year_for_reports(int)

Any idea how I could make this faster? Right now I'm clocking at 555703
ms, which is intolerable.

A couple of things spring to mind, although i don't know what, if
anything, any of these will actually do for performance:

Could retailer ID be stored as an integer? if you needed the hyphens
for display purposes you could use something like this:

SELECT INSERT (

INSERT (
retailer_id,
7,
0,
'-'
),
12,
0,
'-'
)

varchar 255 for session_id sounds very large. Could it be smaller?

month_for_reports is only ever going to be 1-12 so it might as well be
tinyint

other than that, make sure everything is properly indexed - but I guess
you've done that already?
HIH
Nov 15 '06 #3

Heist wrote:
retailer_id cannot be stored as an integer sadly, but session_id could
be smaller. And yes, indexes are seemingly optimal. Thanks for the
answer.
You're welcome. Sorry it wasn't much help.

Nov 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: zeroIWantNoSpamtype | last post by:
Hi, I'm trying to get some old programs ported to standard C++ and I don't fancy having to keep typing "std::", so I would like to enter using namespace std (I forget if there's a semicolon,...
6
by: pragile | last post by:
I have a stored procedure that has some problems with slow performance. The procedure has 2 parameters @firstname varchar(100) @lastname varchar(100) These parameters can have values like a%,...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
4
by: bala | last post by:
hi!!! the following is the sql which is veri slow cos of the 'Not In' clause, would appreciate if u anyone can suggest any other way to bring about the same result SELECT Id, LOC, AGENCY,...
7
by: arndt.blumenthal | last post by:
Hi, i implement a ASP.NET-Application for a customer. Every day the customer has the problem, that the first access to the application takes very long. What could be the reason? Thanks for...
4
by: Steph | last post by:
Hi - Trying to chase down a baffling performance issue. Our database has been running very slow lately. So we are performance tuning the database. In doing so, we created a copy of our...
0
by: phlype.johnson | last post by:
I'm struggling to find the best query from performance point of view and readability for a normalized DB design. To illustrate better my question on whether normalized designs lead to more complex...
10
by: shsandeep | last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development database while it took 2 hours to load in a UAT database. UAT db is partitioned. Dev db is not partitioned. the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.