 |
| Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more. |
|
 |
01-01-2008, 06:26 PM
|
#1 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Purpose of indexing? MySQL Help
I'm making a search engine for my website, and I'm not very familiar with MySQL Optimization.
First question is what does indexing do? Is there anything that's bad about it? (aka Why don't i just index all of my columns).
Second question: Is there anything I can do in PHP code that will optimize my results, is there anything that is bad to do?
Right now my code:
* Splits up the query into words that are 2 characters are more and removes stopwords (me, you, what, etc..)
* for each word: mysql queries a SELECT * FROM table where description LIKE '%$word%'
* stores all results in an array with key='title' value++ (score for the word)
* for each word: mysql queries a SELECT * FROM table where title LIKE '%$word%'
* stores all results in an array with key='title' value += 5 (score for the word)
* mysql queries SELECT * FROM table WHERE title = 'searchQuery'
* stores all results in an array with key='title' value += 10
* foreach results as result it displays the title and the score
|
|
|
01-03-2008, 04:36 AM
|
#2 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
Quote:
Originally Posted by Capo64
First question is what does indexing do? Is there anything that's bad about it? (aka Why don't i just index all of my columns).
|
Index is your table sorted by key order (the fields you've chosen) ... kinda  If you create too many indexes insert and edit operations will became slow (Your table will be resorted many times on every insert). Also it's a waste disk space.
Quote:
Originally Posted by Capo64
Second question: Is there anything I can do in PHP code that will optimize my results, is there anything that is bad to do?
Right now my code:
* Splits up the query into words that are 2 characters are more and removes stopwords (me, you, what, etc..)
* for each word: mysql queries a SELECT * FROM table where description LIKE '%$word%'
* stores all results in an array with key='title' value++ (score for the word)
* for each word: mysql queries a SELECT * FROM table where title LIKE '%$word%'
* stores all results in an array with key='title' value += 5 (score for the word)
* mysql queries SELECT * FROM table WHERE title = 'searchQuery'
* stores all results in an array with key='title' value += 10
* foreach results as result it displays the title and the score
|
You might find this helpful: http://dev.mysql.com/doc/refman/5.1/...xt-search.html
Last edited by nasty.web; 01-03-2008 at 05:01 AM..
|
|
|
01-04-2008, 09:11 PM
|
#3 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Thanks for the help, I'm trying to figure out this MATCH() AGAINST() I can't seem to get it to work the right way.
I have it working OK, but it's not doing well if the person searches for a keyword with a hyphen, and the relevancy is kinda messed up.
|
|
|
01-07-2008, 07:09 AM
|
#4 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Are there illegal characters that need to be replaced in search queries?
|
|
|
01-07-2008, 09:17 AM
|
#5 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
|
|
|
01-09-2008, 06:38 PM
|
#6 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Thanks a lot so far nasty,
Can anyone tell me why this isn't a valid query? This stuff is killing me..
Code:
$query = "SELECT title, description, id,
MATCH(title,description)
AGAINST (
REPLACE('$clean', '-', ' ')
) AS Relevance
FROM items
WHERE
MATCH (
REPLACE(title, '-', ' ')
,
REPLACE(description, '-', ' ')
)
AGAINST (
REPLACE('$clean', '-', ' ')
IN BOOLEAN MODE
)
ORDER BY Relevance DESC";
I know for a fact it's something with the REPLACE(title.... and the REPLACE(description...
because it works without the replace. I don't see what's wrong with it though..
It's not that I don't have an index because it doesn't give me that error. It gives me the error that it's invalid syntax.
|
|
|
01-10-2008, 12:46 AM
|
#7 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
MATCH(column, ... ) requires a column name as its parameter not an expression.
My quick suggestion would be to use two columns in your table for the same field: one for indexing, second for actual data.
|
|
|
01-10-2008, 03:56 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Wouldn't that be a little strenuous? My descriptions are pretty long..
Is there a different solution that I con do on the site?
It would be extremely slow to get the titles and descriptions of all my items in an array, then go through the array to replace them and redo the search, right?
|
|
|
01-10-2008, 04:05 PM
|
#9 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
Quote:
Originally Posted by Capo64
Wouldn't that be a little strenuous? My descriptions are pretty long..
Is there a different solution that I con do on the site?
It would be extremely slow to get the titles and descriptions of all my items in an array, then go through the array to replace them and redo the search, right?
|
why do you think you need an array? I suggest you keeping two columns for the same field in a table. Eg.: title (this one for actual data) and title_idx (this one for indexing).
|
|
|
01-10-2008, 05:52 PM
|
#10 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
yeah, but what about the descriptions, I'm searching through the description too
|
|
|
01-11-2008, 12:32 AM
|
#11 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
Quote:
Originally Posted by Capo64
yeah, but what about the descriptions, I'm searching through the description too
|
Add second column for descriptions as well (I still can't see where you need an array). I believe that disk space is much cheaper than your work, so having additional columns wont hurt much (and it's the easiest and the fastest solution).
|
|
|
01-11-2008, 06:08 PM
|
#12 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Alright thanks,
So I would just add a column like title_idx and description_idx, then make a php page that removes hyphens and stop words from each description & title and adds them to the new columns?
|
|
|
01-11-2008, 06:20 PM
|
#13 (permalink)
|
|
v7n Mentor
Join Date: 07-24-06
Posts: 691
Latest Blog: None
|
Yes,
you can update your whole table with something like
Code:
UPDATE table SET title_idx = REPLACE(title,'-',''), description_idx = REPLACE(description,'-','')
And then on each insert or update you should do exactly the same (just add WHERE clause).
|
|
|
01-11-2008, 06:22 PM
|
#14 (permalink)
|
|
Contributing Member
Join Date: 06-11-07
Posts: 238
Latest Blog: None
|
Ok, Thank you so much for all your help nasty.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 09:59 PM.
© Copyright 2008 V7 Inc Powered by vBulletin Copyright © 2000-2009 Jelsoft Enterprises Limited.
|
|
|