473,654 Members | 3,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select with partial length index??

I have a table, table1, with the field 'areacodepluste lephone'. 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 areacodeplustel ephone =
330' -- no results
I tried this: select name from table1 where left(areacodepl ustelephone,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 2010
Bruce D wrote:
Can I use this index in my select statement?
Yes
I tried this: select name from table1 where areacodeplustel ephone =
330' -- no results
This would return only rows where areacodeplustel ephone is exactly 330,
if row contains any other characters it won't be returned.
I tried this: select name from table1 where left(areacodepl ustelephone,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 areacodeplustel ephone like '330%';
Jul 20 '05 #2
"Aggro" <sp**********@y ahoo.com> wrote in message
news:aH******** *******@read3.i net.fi...
How about:
select name from table1 where areacodeplustel ephone like '330%';


Which is the "proper" way to code this SQL statement?:
1) select name from table1 where left(areacodepl ustelephone,3) = '330'
2) select name from table1 where areacodeplustel ephone 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(areacodepl ustelephone,3) = '330'
2) select name from table1 where areacodeplustel ephone 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**********@y ahoo.com> wrote in message
news:FA******** *******@read3.i net.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 areacodeplustel ephone 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
11595
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 need to display an alert box warning the user that a selection cannot be made, and redisplay the item that was previously selected. What is the most efficient way of doing this? Please email replies to tdk13@perfectsolve.com Thanks in advance.
8
11098
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 and there are only 293,658 records total.
2
5211
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 down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ============================================================================
5
2336
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 PostgresQL is doing a seqscan. I've tried this on a PostgresQL-Serverversion 7.3.4 and 8.0 beta1.
3
3337
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 a lot of overhead and I would like to move the overhead to the remote client's PC, using javascript. Thank you for your assistance. Respectfully,
6
8335
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 interchange the data's in the select box. (provuide coding for no scrollbar in select box) Thank You in advance
15
7719
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 code would either select the next value in the member list or the single value. The original post can be found here. This is the code gits helped me write: for (var i = 0; i < attribList.id.length; i++) { var attrib = {};
2
1667
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 specific site. Also he has the option to check a checkbox "Remeber this choice" so that when he returns to the site it remembers what he has choosen from the drop down list and automatically takes him to a specific site. Here is my code:
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
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,...
0
8816
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, 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...
1
8494
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,...
1
6162
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
5627
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
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
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
2
1597
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.