Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
msql query , is this the correct way?
Old 02-09-2009, 07:45 AM msql query , is this the correct way?
Banned

Posts: 34
Trades: 0
hey guys, i am doing a search that gets info from about 3 tables, lets say tblproduct,tbldetails,tblpdf
i am doing this :
select tblproduct.id,tblproduct.title,tbldetail.price,tbl detail.id,tblpdf.id,tblpdf.pdf from tblproduct,tblpdf,tbldetail where tblproduct.id = tbldetail.id and tblproduct.title like '%&querystring%'

i will add more "like statements" depending on what the user clicks.
is this correct way??
Necaxa is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-09-2009, 08:39 AM Re: msql query , is this the correct way?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It's a good start, but as you add more elements to your where clause, it will get slower and slower.
Beside, a filtering on a varchar field (the "like" predicat) cannot use indexes if you start to pattern with '%', so it will need to read each lines of your table to find the corresponding elements.
You better know that from the start.

Sometimes, using an union query might be better, as the query gets more complicated the more you extends your search to more and more fields:
Code:
select 
  tblproduct.id,
  tblproduct.title,
  tbldetail.price,
  tbl detail.id,
  tblpdf.id,
  tblpdf.pdf 
from tblproduct,tblpdf,tbldetail 
where tblproduct.id = tbldetail.id 
and tblproduct.title like '%&querystring%'

union

select 
  tblproduct.id,
  tblproduct.title,
  tbldetail.price,
  tbl detail.id,
  tblpdf.id,
  tblpdf.pdf 
from tblproduct,tblpdf,tbldetail 
where tblproduct.id = tbldetail.id 
and tbldetail.origin like '%country%'
An union is simply several different queries, that all return the same type and amount of column that are added each to the other.

2 more things:
1) In a case like this country search I improvised, you can speed it up a lot by giving a predefined list of countries to your search pages, and use it's reference (iso abbreviation, number..) rather than doing a wildcard search on the varchar value.
2) Take care of sql injection. When I see %&querystring% in your original query, I fear that you take the input of the user without validating it.
If it's the case, it could interfere with your database badly.
http://xkcd.com/327/
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-09-2009, 11:56 AM Re: msql query , is this the correct way?
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
hehe i love that comic.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 02-09-2009, 01:58 PM Re: msql query , is this the correct way?
Banned

Posts: 34
Trades: 0
thanks for reply,
just quick review - so if i use "like", i cant use indexes..
ok so what technique do largeish sites use??
union - inner join
Necaxa is offline
Reply With Quote
View Public Profile
 
Old 02-09-2009, 02:48 PM Re: msql query , is this the correct way?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
so if i use "like", i cant use indexes..
it's not the like that's the problem.
The problem occurs when your like patterns starts with a wildcard (%).
If it's not the case, then the db engine can use the index.

Quote:
ok so what technique do largeish sites use??
mostly, normalizing as much as they do, and don't make searches on varchar columns with a starting wildcards.

A cluster of servers can help too, as caching the search results to reuse the result during a couple of minutes.

Another thing that can help is to build a sort of map of frequently used terms, and prebuild the results against them.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to msql query , is this the correct way?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22388 seconds with 12 queries