473,472 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Select with partial length index??

I have a table, table1, with the field 'areacodeplustelephone'. This field
is of length 10. I created an index called 'areacode' that only uses the
first three characters.
Can I use this index in my select statement?
I tried this: select name from table1 where areacodeplustelephone =
330' -- no results
I tried this: select name from table1 where left(areacodeplustelephone,3) =
'330' -- got the results but slow...id didn't use the index

I'm I going about this the wrong way? Is there a better way than using the
"left" function?

Any help is greatly appreciated.

-bruce duncan
Jul 20 '05 #1
4 2000
Bruce D wrote:
Can I use this index in my select statement?
Yes
I tried this: select name from table1 where areacodeplustelephone =
330' -- no results
This would return only rows where areacodeplustelephone is exactly 330,
if row contains any other characters it won't be returned.
I tried this: select name from table1 where left(areacodeplustelephone,3) =
'330' -- got the results but slow...id didn't use the index


Yes, because by using that function, you force the MySQL to use the
function for all the rows.

How about:
select name from table1 where areacodeplustelephone like '330%';
Jul 20 '05 #2
"Aggro" <sp**********@yahoo.com> wrote in message
news:aH***************@read3.inet.fi...
How about:
select name from table1 where areacodeplustelephone like '330%';


Which is the "proper" way to code this SQL statement?:
1) select name from table1 where left(areacodeplustelephone,3) = '330'
2) select name from table1 where areacodeplustelephone like '330%'

Is there a lot of overhead on the DB using the left() function?
I thought MySQL wouldn't use indexes on a "like" clause?
Or should I just run a bunch of tests to see which is faster? I will be
searching 200 million rows...so performance is key.

-bruce
Jul 20 '05 #3
Bruce D wrote:
Which is the "proper" way to code this SQL statement?:
1) select name from table1 where left(areacodeplustelephone,3) = '330'
2) select name from table1 where areacodeplustelephone like '330%'
In this case I suggest using the 2)
Is there a lot of overhead on the DB using the left() function?
Like I said, if you use it, MySQL has to check all rows in the table, so
yes, there is a lot of overhead. I don't know if you are a programmer,
but as I am, I think it is pretty obvious why MySQL doesn't use indexes
if the function is used. MySQL would require a lot of more if-sentences
to slow down queries, if it would have to add logic for detecting the if
parameter for cuntion is a string from column, and also detecting if the
result of that function is left part of the column.

But when you use "like", the MySQL instantly knows that the left part of
the if is a string from column. So there is no problem with that part.
I thought MySQL wouldn't use indexes on a "like" clause?
It doesn't, if you have like '%something', but in your case the % is at
the end, so MySQL can use indexes.
Or should I just run a bunch of tests to see which is faster? I will be
searching 200 million rows...so performance is key.


I think one try of each should give you a pretty good image which is
faster. If the like doesn't use indexes either, you either don't have
index on that column, or you have something else which is not allowing
the MySQL to use indexes.
Jul 20 '05 #4
"Aggro" <sp**********@yahoo.com> wrote in message
news:FA***************@read3.inet.fi...
I think one try of each should give you a pretty good image which is
faster. If the like doesn't use indexes either, you either don't have
index on that column, or you have something else which is not allowing
the MySQL to use indexes.


Thanks for the info. I assumed the left() function had a lot of overhead,
but I wanted to hear it from someone else.

I've tried both queries running against 1 million records.

select name from table1 where areacodeplustelephone like '330%' works the
best.

MySQL was not using the index I had created...and I have no idea why. So, I
issue the "force index (areacode)" and it speeds up the query by almost 40%.

Looks like this is the best solution for this situation. Thanks again for
your input...it was very helpful!

-bruce duncan
Jul 20 '05 #5

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

Similar topics

8
by: asd | last post by:
I need to find the value/index of the previously selected item of a select list. That is, when the user selects an item from the list and a certain condition elsewhere in the form is not met, I...
8
by: Rich | last post by:
My table looks like this: char(150) HTTP_REF, char(250) HTTP_USER, char(150) REMOTE_ADDR, char(150) REMOTE_HOST, char(150) URL, smalldatetime TIME_STAMP There are no indexes on this table...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
5
by: Silvio Matthes | last post by:
Hello, I'm new to the list and did not find a suitable answer to my question so here it is: I try to select the rows of a table where the content of a varchar-column is empty ('') and...
3
by: ANTISPAM_garycnew_ANTISPAM | last post by:
What is the simplest way to retain the last option value selected in an html select object using javascript? I am currently using a server-side cgi language to accomplish this task, but it adds...
6
by: rajesh | last post by:
Is it possible to display the select box without scrollbar in my program there is a need for that . The code attached below contains 2 select box and four buttons and the button is used to...
15
RMWChaos
by: RMWChaos | last post by:
As usual, an overly-long, overly-explanatory post. Better too much info than too little, right? A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my...
2
by: printline | last post by:
Hi all I have a problem with a form script. The form script should do the following: When a user selects a value from a drop down list and hits the "next" button it should take him to a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.