This query is running REAL slow ... like 1.2 secs ... any ideas on how
I could optimize it? Perhaps my indexes are incorrect?
$this->query = "SELECT m.username as username,
e.title as title,
e.exhibition_id as
exhibition_id,
LEFT(e.text,50) as
text,
e.random_key,
e.server_id,
e.datetime_crea ted as
datetime,
e.views as views,
a.title as
album_title,
a.album_id as
album_id,
o.subdomain as
subdomain,
ss.symbol as symbol,
COUNT(DISTINCT
erg.exhibitions _rating_general _id) AS num_ratings,
AVG(erg.value) AS
rating,
COUNT(DISTINCT
ef.exhibitions_ favorite_id) AS num_favorites,
COUNT(DISTINCT
c.comment_id) AS num_comments
FROM ".$CONFIG['tbl_exhibition s']." e,"
.$CONFIG['tbl_albums']." a,"
.$CONFIG['tbl_members']." m,"
.$CONFIG['tbl_organizati ons']." o,"
.$CONFIG['tbl_s_symbols']." ss
LEFT JOIN
".$CONFIG['tbl_exhibition s_rating_genera l']." erg
ON erg.exhibition_ id = e.exhibition_id
LEFT JOIN
".$CONFIG['tbl_exhibition s_favorites']." ef
ON ef.exhibition_i d = e.exhibition_id
LEFT JOIN ".$CONFIG['tbl_comments']." c
ON ( c.reference_id = e.exhibition_id
AND
c.type_id = 4 )
WHERE m.organization_ id = $organization_i d
AND e.album_id = a.album_id
AND e.datetime_crea ted > now() -
interval 50 day
AND m.organization_ id =
o.organization_ id
AND m.symbol_id = ss.symbol_id
AND e.member_id = m.member_id
AND (e.scope_id = 5 OR e.scope_id = 3)
AND e.active = $active
AND m.active = $active
GROUP BY erg.exhibition_ id
ORDER BY $order $sort";
+-------+--------+-----------------------------------+---------------+---------+-----------------+-------+---------------------------------+
| table | type | possible_keys | key |
key_len | ref | rows | Extra |
+-------+--------+-----------------------------------+---------------+---------+-----------------+-------+---------------------------------+
| o | const | PRIMARY | PRIMARY |
2 | const | 1 | Using temporary; Using filesort |
| e | ALL | member_id | NULL |
NULL | NULL | 12034 | Using where |
| a | eq_ref | PRIMARY | PRIMARY |
2 | e.album_id | 1 | |
| m | eq_ref | PRIMARY,symbol_ id,organization _id | PRIMARY |
3 | e.member_id | 1 | Using where |
| ss | eq_ref | PRIMARY,symbol_ id | PRIMARY |
2 | m.symbol_id | 1 | |
| erg | ref | exhibition_id | exhibition_id |
4 | e.exhibition_id | 1 | |
| ef | ref | exhibition_id | exhibition_id |
4 | e.exhibition_id | 1 | |
| c | ref | type_id,referen ce_id | reference_id |
4 | e.exhibition_id | 10 | |
+-------+--------+-----------------------------------+---------------+---------+-----------------+-------+---------------------------------+
CREATE TABLE journals (
journal_id smallint(10) NOT NULL auto_increment,
member_id smallint(7) NOT NULL default '0',
title varchar(100) NOT NULL default '',
text text NOT NULL,
reference_link varchar(100) NOT NULL default '',
location varchar(100) NOT NULL default '',
scope_id tinyint(4) NOT NULL default '5',
group_id1 int(7) NOT NULL default '0',
group_id2 mediumint(7) NOT NULL default '0',
group_id3 int(7) NOT NULL default '0',
datetime_create d datetime NOT NULL default '0000-00-00 00:00:00',
datetime_modifi ed datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (journal_id),
KEY group_id1 (group_id1,grou p_id2,group_id3 ),
KEY member_id (member_id),
FULLTEXT KEY text (text)
) TYPE=MyISAM;
CREATE TABLE `comments` (
`comment_id` int(10) NOT NULL auto_increment,
`member_id` int(10) NOT NULL default '0',
`reference_id` int(10) NOT NULL default '0',
`title` varchar(25) NOT NULL default '',
`text` text NOT NULL,
`datetime_creat ed` datetime NOT NULL default '0000-00-00 00:00:00',
`datetime_modif ied` datetime NOT NULL default '0000-00-00 00:00:00',
`root_id` int(10) default NULL,
`type_id` char(1) NOT NULL default '',
`path` text NOT NULL,
PRIMARY KEY (`comment_id`),
KEY `member_id` (`member_id`),
KEY `root_id` (`root_id`),
KEY `type_id` (`type_id`,`dat etime_created`) ,
KEY `reference_id` (`reference_id` ,`type_id`)
) TYPE=MyISAM AUTO_INCREMENT= 162949 ; 2 1399
ensnare wrote: This query is running REAL slow ... like 1.2 secs ... any ideas on how I could optimize it? Perhaps my indexes are incorrect?
It looks like you're trying to be too clever, using outer joins to find
a way to compute several aggregates at the same time as getting
non-aggregate data results. Don't do that!
Execute a separate query for each of your aggregate computations. That
is, pull the COUNT() and AVG() aggregates out of that monster query, and
do them each as a separate query.
Your SQL will become much easier to understand, much easier to maintain,
and I'm betting it will run much faster as separate queries than as one
hopelessly overcomplicated query.
Regards,
Bill K.
"Bill Karwin" wrote: ensnare wrote:
This query is running REAL slow ... like 1.2 secs ... any ideas
on how I could optimize it? Perhaps my indexes are incorrect? It looks like you’re trying to be too clever, using outer joins to find a way to compute several aggregates at the same time as getting non-aggregate data results. Don’t do that!
Execute a separate query for each of your aggregate computations. That is, pull the COUNT() and AVG() aggregates out of that monster
query, and do them each as a separate query.
Your SQL will become much easier to understand, much easier to maintain, and I’m betting it will run much faster as separate queries than as one hopelessly overcomplicated query.
Regards, Bill K.
I second Bill. This is the most complex query I have seen in my life.
Break it apart and time the components. You would have a much easier
time optimizing each component (building indecis, etc.). That is
easily done if you are running the query from a script (e.g. php), and
you will negligible performance penalty from the breakup.
-- http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/mySQL-Query-...ict139345.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=466919 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ensnare |
last post by:
This query is running REAL slow ... like 1.2 secs ... any ideas on how
I could optimize it? Perhaps my indexes are incorrect?
$this->query = "SELECT m.username as username,
e.title as title,
e.exhibition_id as
exhibition_id,
LEFT(e.text,50) as
text,
e.random_key,
|
by: AC Slater |
last post by:
Whats the simplest way to change a single stored procedures query
optimization level?
In UDB8 that is.
/F
|
by: Sean C. |
last post by:
Helpful folks,
I have recently migrated our test server, which runs Win NT 4, from
V7.2 FP11 to V8.1.3. Just about everything works wondefully, except I
am having major problems getting the previously defined federated
servers/nicknames to work. But I will start a different thread about
that problem. I thought I'd ask about the less critical problem first.
It deals with the Control Center and the following error:
SQL0713N The...
|
by: Eugene |
last post by:
I am trying to set query optimization class in a simple SQL UDF like
this:
CREATE FUNCTION udftest ( in_item_id INT )
SPECIFIC udftest
MODIFIES SQL DATA
RETURNS TABLE( location_id INT, period_id INT )
BEGIN ATOMIC
SET CURRENT QUERY OPTIMIZATION 1;
|
by: WantedToBeDBA |
last post by:
Hi all,
db2 => create table emp(empno int not null primary key, \
db2 (cont.) => sex char(1) not null constraint s_check check \
db2 (cont.) => (sex in ('m','f')) \
db2 (cont.) => not enforced \
db2 (cont.) => enable query optimization)
DB20000I The SQL command completed successfully.
db2 => insert into emp values(1,'m')
| |
by: 73blazer |
last post by:
We are migrating a customer from Version 7.1 FP3, to Version 8.2 (8.1 FP8).
For the most part, things are faster, but there is one query that is
much much slower, and it is a query that is used all the time.
select ATTR1,ATTR2,ATTR3,ATTR4 from physical.part_list
where S_PART_NUMBER like '%KJS%'
The widlcard before and after seems to be hosing it, but for this
particular piece of the application, this type of query is neccessary.
|
by: UnixSlaxer |
last post by:
Hello,
Running a query for the first time on DB2 takes a fixed amount of time.
But when query is executed for the second time, the amount of time is
usually less since the query is (most probably) cached already.
I would like to clear out the DB2-UDB 8.2 query cache (I want the
previous execution time again).
Any advice would be appreciated.
|
by: Bernard Dhooghe |
last post by:
To retrieve data from a query where multiple rows can be returned, a
cursor can be used.
Different programming interface exist for cursors: embedded SQL, CLI,
SQL PL, SQLJ, JDBC.
I we look at the CLI interface, as a statement is first prepared in CLI
before a cursor is associated with it, the cursor attributes are not
known at prepare time. Does it mean that the query path for retrieving
rows is not dependent of the cursor attributes...
|
by: ADezii |
last post by:
One frequently asked question at TheScripts is "Should I use a Stored Query or an SQL Statement in those situations that require a Query (RecordSets, RecordSources, Append, Delete, Update Operations, etc.)?" The response, in virtually all but a few circumstances, is that you should use a Stored Query in place of the parallel SQL Statement. The brief explanation that follows will explain the reasoning behind this:
When you store a Query as a...
|
by: Don Li |
last post by:
Hi,
Env: MS SQL Server 2000
DB Info (sorry no DDL nor sample data):
tblA has 147249 rows -- clustered index on pk (one key of
datatype(int)) and
has two clumns, both are being used in joins;
intersecTbl4AB has 207016 rows -- clustered index on two fks and
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |