473,808 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to optimise this query

Hi

I have this query:

SELECT id, name, YEAR(born) AS year
FROM people
WHERE DAYOFMONTH(born ) = 7
AND MONTH(born) = 12
ORDER BY year DESC, name

operating on this table:

CREATE TABLE `people` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`born` date NOT NULL default '0000-00-00',
`died` date NOT NULL default '0000-00-00',
`imdb` int(11) NOT NULL default '0',
`allmusic` varchar(25) NOT NULL default '',
`allmovies` varchar(25) NOT NULL default '',
`uri` varchar(100) NOT NULL default '',
`image` blob NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `imdb` (`imdb`),
KEY `born` (`born`),
KEY `died` (`died`),
FULLTEXT KEY `name_2` (`name`)
) TYPE=MyISAM AUTO_INCREMENT= 314541 ;

which has 169,549 rows.

EXPLAIN SELECT shows that it has type ALL, and no possible keys. It
shows 'using where, using filesort'. The query is quite slow.

How could I optimise this query?
Thanks in advance

Jasper Bryant-Greene

Jul 20 '05 #1
1 1915
Jasper Bryant-Greene wrote:
SELECT id, name, YEAR(born) AS year
FROM people
WHERE DAYOFMONTH(born ) = 7
AND MONTH(born) = 12
ORDER BY year DESC, name


I think this is a similar problem as trying to use an index for a query
like this:
SELECT ... FROM people WHERE allmusic LIKE '%foo';

The database cannot create an index for an arbitrary trailing substring.
Likewise, it cannot create an index on a calculation of a numeric or
date column. An index can be defined for simply a column name, not an
expression on that column (though the "expression index" is a feature of
some other RDBMS products, e.g. Firebird, so it is in fact possible).

To use an index for this query, you are going to have to store redundant
columns for DAYOFMONTH and MONTH. That is:

ALTER TABLE ADD COLUMN day_of_month_bo rn integer;
ALTER TABLE ADD COLUMN month_born integer;
UPDATE TABLE SET
day_of_month_bo rn = DAYOFMONTH(born ),
month_born = MONTH(born);
CREATE INDEX people_domb ON people(day_of_m onth_born);
CREATE INDEX people_mb ON people(month_bo rn);

Then do your query using the columns that have indexes on them:

SELECT id, name, YEAR(born) AS year
FROM people
WHERE day_of_month_bo rn = 7
AND month_born = 12
ORDER BY year DESC, name

Regards,
Bill K.
Jul 20 '05 #2

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

Similar topics

3
2509
by: dr zoidberg | last post by:
Hello, $a = mysql_query("Select a,b FROM t WHERE category=1") while($a) { $x = $a; //some echo //another query $b = mysql_query("Select * FROM t WHERE subcategory=$x") while ($b) {
1
1731
by: ben | last post by:
I have been using a nasty combination of php and mysql to generate a narrow down by attribute bar as seen on the likes of shopping.com. For example a user could select 4X Zoom to narrow down a selection of digital cameras. The current method I use is to loop though each one of the attributes and their values and run a separate query of each attribute value: for($i=0; $i < attribute_count; $i++) {
0
1374
by: Wouter | last post by:
Hi, I have a database like this: CREATE TABLE weersgeschiedenis ( WMO enum('06235','06260','06280','06290','06310','06380') NOT NULL default '06235', YYYYMMDD date NOT NULL default '0000-00-00', DDVEC smallint(5) unsigned NOT NULL default '0', FG tinyint(3) unsigned NOT NULL default '0',
0
482
by: Jasper Bryant-Greene | last post by:
Hi I have this query: SELECT id, name, YEAR(born) AS year FROM people WHERE DAYOFMONTH(born) = 7 AND MONTH(born) = 12 ORDER BY year DESC, name
5
1449
by: Robin Tucker | last post by:
I have these 3 queries - they are the same except each fetches record counts for one of 3 different record types, nSubsets (type 0), nAssets (type 1) and nImages (type 2). Is there any way I could get all 3 of these (based on the Node.Type integer) with a single query? IF @Error = 0 BEGIN SELECT @nSubsets = COUNT(*) FROM Node INNER JOIN Adjacency
3
5395
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says there is a sample file called Ixtrasp.asp, but I could not find it in my system although I installed indexing service. I followed the steps in MSDN site to create a basic .asp query form (too long to post it here), but it always displays: No...
6
1187
by: Driesen via SQLMonster.com | last post by:
Hi guys Is there any way I can run this query faster? Should I take out the ORDER BY clause? This is supposed to return 17,000 rows and takes around 30 minutes or so. Is there no way at all to get this result faster? select r.AttorneyName, r.sitename, r.applicationid, r.clientsurname, r. clientinitials, r.clientidno, r.grantedamount, r.bankname, r.accountnumber, r.status, r.grantdate, r.consultantname, r.propertyaddress,...
2
1636
by: Hervé Piedvache | last post by:
Hi, I have may be a stupid question, but I'm a little surprised with some explains I have, using date fields ... I would like to understand exactly when index are used ... I'm using PostgresQL 7.4.1 I have a table with 351 000 records. I have about 300 to 600 new records by day
5
1361
by: Aussie Rules | last post by:
Hi, I have a vb.net 2005 project that has just got slower and slower as I develop. Does anybody know of a code tool to use to to pin point performance problems, and clean up/optimise the code (remove dead code etc). Thanks
0
10374
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 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...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9196
agi2029
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...
1
7651
isladogs
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...
0
6880
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();...
0
5548
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...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
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...

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.