Advanced MySQL Joining. Speeding up query

后端 未结 3 768
春和景丽
春和景丽 2021-01-23 01:56

I have a list of users on my php application (using codeigniter). Each user may have completed a form with about 1000 or so total fields. The structure looks similar to this:

相关标签:
3条回答
  • 2021-01-23 02:33

    I'm not sure I completely understand the problem. You can use a single query to get all users, and all of their fields (basically, this is what Philip suggested):

    SELECT u.username, f.* 
    FROM user AS u
        INNER JOIN completed_form_fields AS f
        ON f.userid = u.id
    ORDER BY u.id, f.fieldkey
    

    To filter the results, add a WHERE clause with the conditions. For example, to only get data from fieldkeys 'k1', 'k2' and 'k3':

    SELECT u.username, f.* 
    FROM user AS u
        INNER JOIN completed_form_fields AS f
        ON f.userid = u.id
    WHERE f.fieldkey IN ('k1', 'k2', 'k3')
    ORDER BY u.id, f.fieldkey
    

    Is this what you're looking for?

    0 讨论(0)
  • 2021-01-23 02:40

    There is a way but it gets over expensive the more you add fields. The same thing occurs with many CMS that choose to store additionnal user data in that form.

    You can get a working search SQL using this:

    SELECT
        users.*,
        firstname.data AS firstname,
        lastname.data AS lastname,
        eyecolor.data AS eyecolor,
    
    FROM
        users
        LEFT JOIN completed_form_fields AS firstname ON firstname.userid = users.id AND firstname.fieldkey = "firstname"
        LEFT JOIN completed_form_fields AS lastname ON lastname.userid = users.id AND lastname.fieldkey = "lastname"
        LEFT JOIN completed_form_fields AS eyecolor ON eyecolor.userid = users.id AND eyecolor.fieldkey = "eyecolor"
    
    WHERE
        firstname.data LIKE '%searchdata%'
        OR lastname.data LIKE '%searchdata%'
        OR eyecolor.data LIKE '%searchdata%'
    

    This method gets very big and expensive for the MySQL server the more you add tables. Therefore, i would recommend not to go more than 10-15 joins like that and then again, i'd profile it to make sure.

    0 讨论(0)
  • 2021-01-23 02:43
    SELECT u.username, f.formid, f.userid, f.fieldkey, f.data FROM user AS u
    LEFT JOIN completed_form_fields AS f
    ON f.userid = u.id
    
    
    you should look at indexing your userid, index(userid), via phpmyadmin or sql file
    

    mysql-indexes

    0 讨论(0)
提交回复
热议问题