Search 'Algorithm'

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Search 'Algorithm'

Post by LiveFree »

Ok so maybe it's not an 'algorithm' so-to-speak but here is how I do it:

I get 10 rows from a DB (pagination), I then loop through each one with a preg_match looking like:

Code: Select all

while ($row = mysql_fetch_array($sql)){
if (preg_match("#(.*?)$name(.*?)#i", $row['name'])) $points += 1;

if ($points >= $total_points (2)) $hits[] = $row['id'];
So basically, if the preg finds the entered text anywhere in the DB record, it adds a point and if the points are greater than 2, that record is considered a 'hit'.

Can anyone tell me how I might go about improving this?

Thanks
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

I'm no search expert but I know there's no future in preging through everything for matches. What DB are you using?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

MySQL :D
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Is there any reason the MySQL full text Indexes and search functionality won't work for you?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Isnt it true that Fulltext keys will slow down the querying on that column?
User avatar
johno
Forum Commoner
Posts: 36
Joined: Fri May 05, 2006 6:54 am
Location: Bratislava/Slovakia
Contact:

Post by johno »

Tucker wrote:Isnt it true that Fulltext keys will slow down the querying on that column?
Actually all indexes are made to make searching a lot faster. Where do you have that they slow things down?

Indexes make inserts/updates/deletes slower, but not selects.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Yeah, I've never heard that indexes slowing down querying, fulltext or otherwise. The point of them is to speed things up, and they are seperate from the tables and used when necessary.

What it would slow down is that they have to be maintained, so on insert and update the index needs to also be updated, but these are typically few and far between. Selects performance is what you typically want to improve...

MySQL full-text searches are much faster and more powerful and than any search algorithm you're going to put together with regexes, unless of course you're only trying to search for that one word in a 10 row result set as demoed in your code.
Post Reply