Jump to content

Optimize query


The Little Guy

Recommended Posts

Is there a faster way to search my database? I only want a maximum of of 500 rows, but I also want to show how may rows could have been returned. (similar to google).

 

SELECT SQL_CALC_FOUND_ROWS
  title,
  content,
  URL,
  id
FROM $searchType
  WHERE
    MATCH(URL,title,content) AGAINST ('$searchQuery' IN BOOLEAN MODE) AND
content IS NOT NULL
LIMIT $limitvalue, $limit

Link to comment
Share on other sites

Run the EXPLAIN on it and see if you get any thing fishy in it

 

Indexing title content, url may aid the queries (assuming id is PK)

 

Also I believe if you move the content IS NOT NULL first the query will reduce the total number of rows to query against for the MATCH portion which could help also.  I've been told mysql reads like a book left to right top to bottom in a sense.

 

 

Don't index content if its a large text area

Link to comment
Share on other sites

 SELECT SQL_CALC_FOUND_ROWS
title,
content,
URL,
id
FROM $searchType
WHERE 
(`title` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci)
OR
`content` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci
OR 
`URL` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci
LIMIT $limitvalue , $limit 

 

This seems runs quicker.

 

Another question... is there a way I can make something that shows how close a search result matches a search query?

Link to comment
Share on other sites

Yes and no this is something I like to do more on the php end of things because I can simply say  here is the keywords here is how many keywords that row had and do a division for each match.

 

Google it because what I'd suggest in mysql would be probably slow

 

 

Did you do

 

EXPLAIN

 

SELECT....

 

to see how it looks?

Link to comment
Share on other sites

go in phpmyadmin (fill in all php variables with static values)

 

and do

EXPLAIN
SELECT SQL_CALC_FOUND_ROWS
title,
content,
URL,
id
FROM $searchType
WHERE 
(`title` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci)
OR
`content` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci
OR 
`URL` LIKE CONVERT( _utf8 '%$searchQuery%' USING latin1 ) COLLATE latin1_swedish_ci
LIMIT $limitvalue , $limit 

 

See what comes up

Link to comment
Share on other sites

Here is my FULL-TEXT Search:

 

$query_count = sprintf("SELECT SQL_CALC_FOUND_ROWS
  title,
  content,
  URL,
  id
FROM %s
  WHERE
    MATCH(
	`URL`,
	`title`,
	`content`
) AGAINST ('%s' IN BOOLEAN MODE) AND
content IS NOT NULL
LIMIT $limitvalue, $limit",
mysql_real_escape_string($searchType),
mysql_real_escape_string($searchQuery));

$sql = mysql_query($query_count)or die(mysql_error()); 
$result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error());
$total = mysql_fetch_array($result_count);
$totalrows = $total[0];

Link to comment
Share on other sites

 

I know, that is what I use in my query.

 

If I remove that, and SQL_CALC_FOUND_ROWS would it speed up the query?

 

Didn't notice... sorry :P

 

It probably would speed it up. Although it still is faster, than running a separate query to get number of rows.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.