Jump to content

Multicolumn index and searching


ejaboneta

Recommended Posts

I'll try to provide as much details as possible. I'm creating a database of people with dynamic fields. To achieve this, I've created tables for `people`,`fields` and `values`.

  • people: has an id and name
  • fields: has an id and field name
  • values: has an id, peopleid, fieldid, value, and delta(because there may be multiple values for certain fields)

 

 

The index on the `values` table is set up with peopleid,fieldid,delta.

 

Question 1: is this the best way to index the table?

 

Question 2: if I wanted to search the table for first name(fieldid=0) and last name(fieldid=1), is this the best way to do it?

SELECT f0.peopleid FROM `values` f0 JOIN `values` f1 ON f0.peopleid=f1.peopleid WHERE (f0.fieldid= 0 AND f0.value='John') AND (f1.fieldid= 1 AND f1.value='Doe')

Please also let me know if there is a better way to structure the database and any other tips. Thank you.

Link to comment
https://forums.phpfreaks.com/topic/280629-multicolumn-index-and-searching/
Share on other sites

you can optimize your SQL queries by checking that how MySQL executes your queries and that can be done by using EXPLAIN statement

 

refer: http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

 

so when you index your table use EXPLAIN statement to check its possible keys, rows and other details

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.