Jump to content

faster queries


The Little Guy

Recommended Posts

OK, how could I improve performance?

 

My first query gets all the results, so I know how many pages to have

 

My second query is to get the results that will be shown on the page.

 

<?php
// Query to get ALL valid results
$query_count = "SELECT title,content,URL, 
MATCH(URL) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score1,
MATCH(title) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score2,
MATCH(content) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score3 
FROM $searchType WHERE MATCH(title) AGAINST ('$searchQuery' IN BOOLEAN MODE) OR
MATCH(URL) AGAINST ('$searchQuery' IN BOOLEAN MODE) OR
MATCH(content) AGAINST ('$searchQuery' IN BOOLEAN MODE)";
$result_count = mysqli_query($db,$query_count);    
$row = mysqli_fetch_array($result_count);  
$totalrows = mysqli_num_rows($result_count);

$limitvalue = $page * $limit - ($limit);  

$time_start = microtime(true);
// Same query as above, only it returns a limited number of results ($limit = 5 to 100)
$query = "SELECT title,content,URL,id,
MATCH(URL) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score1,
MATCH(title) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score2,
MATCH(content) AGAINST ('$searchQuery' IN BOOLEAN MODE) AS score3 
FROM $searchType WHERE MATCH(title) AGAINST ('$searchQuery' IN BOOLEAN MODE) OR
MATCH(URL) AGAINST ('$searchQuery' IN BOOLEAN MODE) OR
MATCH(content) AGAINST ('$searchQuery' IN BOOLEAN MODE)
ORDER BY score1 DESC, score2 DESC, score3 DESC LIMIT $limitvalue, $limit";

?>

 

I don't know if it is just me, but It seems like this would take a while to query, if it needed to query 100,000,000+ rows in a database.

 

What would the fastest way to do this be?

Link to comment
Share on other sites

hm, if you're going to be consistently querying MATCHING AGAINST 100,000,000+ records you're probably going to need a multi-sever solution. at very least you're going to want to hold your data in RAM and not hit the hard drive for each query.

 

otherwise, i think you're doing what's necessary to determine the total number of records and then select a subset.

Link to comment
Share on other sites

<?php
$query_count = "SELECT SQL_CALC_FOUND_ROWS
  title,
  content,
  URL,
  id,
    (MATCH(URL) 
    AGAINST ('%$searchQuery%' IN BOOLEAN MODE) * 1.5) +
  (MATCH(title) 
    AGAINST ('%$searchQuery%' IN BOOLEAN MODE) * 0. +
  (MATCH(content) 
    AGAINST ('%$searchQuery%' IN BOOLEAN MODE) * 0.5) AS score
FROM $searchType
  WHERE
    MATCH(title, URL, content)
      AGAINST ('%$searchQuery%' IN BOOLEAN MODE)
AND content != ''
ORDER
  BY score DESC LIMIT $limitvalue, $limit";  

$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];
?>

 

OK... I am down to one SQL statement, but... it still seems to run slow on 4,420 recods... what should I do?

 

If I do a search, something with the most results (free), it takes 1.619 Seconds to do just the query.

 

I was thinking, would it be faster to do a search on 3 tables instead of one?

 

- One with the URL's

- One with the titles

- One with the content

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.