Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a big mysql database with relevance

Tags:

I'm building a rather large "search" engine for our company intranet, it has 1miljon plus entries it's running on a rather fast server and yet it takes up to 1 min for some search queries.

This is how the table looks

Table

I tried create an index for it, but it seems as if i'm missing something, this is how the show index is showing

idexes

and this is the query itself, it is the ordering that slows the query mostly but even a query without the sorting is somewhat slow.

SELECT SQL_CALC_FOUND_ROWS *
FROM `businessunit`
INNER JOIN `businessunit-postaddress` ON `businessunit`.`Id` = `businessunit-postaddress`.`BusinessUnit`
WHERE `businessunit`.`Name` LIKE 'tanto%'
ORDER BY `businessunit`.`Premium` DESC ,
CASE WHEN `businessunit`.`Name` = 'tanto'
THEN 0
WHEN `businessunit`.`Name` LIKE 'tanto %'
THEN 1
WHEN `businessunit`.`Name` LIKE 'tanto%'
THEN 2
ELSE 3
END , `businessunit`.`Name`
LIMIT 0 , 30

any help is very much appreciated

Edit: What's choking this query 99% is ordering by relevance with the wildcharacter % When i Do an explain it says using where; using fsort

like image 661
Breezer Avatar asked Jan 28 '13 11:01

Breezer


People also ask

How do I search an entire database in MySQL?

Find data across a MySQL connection by using the text search feature on any number of tables and schemas. From the schema tree, select the tables, schemas, or both to search and then right-click the highlighted items and click Search Data Table from the context menu.

How do I find large tables in MySQL?

To get largest table in MySQL database (of all databases) use: SELECT table_name AS "Table", round(((data_length + index_length) / 1024 / 1024), 2) "Table size in MB" FROM information_schema. TABLES order by data_length+index_lenght desc limit 1; These queries may take time based on number of tables.

What is considered a large MySQL database?

In addition, a practical size limit on MySQL databases with shared hosting is: A database should not contain more than 1,000 tables; Each individual table should not exceed 1 GB in size or 20 million rows; The total size of all the tables in a database should not exceed 2 GB.

Can MySQL be used for large databases?

Yes, You can create large-scale applications using PHP and MySQL. You need to use some other helper tools as well, which will help scaling your app, for example load balancers.


2 Answers

You should try sphinx search solution which is full-text search engine will give you very good performance along with lots of options to set relevancy.

Click here for more details.

like image 53
Minesh Avatar answered Dec 27 '22 06:12

Minesh


Seems like the index doesn't cover Premium, yet that is the first ORDER BY argument.

Use EXPLAIN your query here to figure out the query plan and change your index to remove any table scans as explained in http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

like image 44
jakber Avatar answered Dec 27 '22 07:12

jakber