I am not very good at MySQL, so I often find that I am preparing suboptimal queries that work, but I know that this is terribly inefficient. I hope you guys could give me some guidance on why the next request does not work, and what methods should I use to execute such requests.
I have the following table structure:
TABLE Files files_id => INT(12), PRIMARY, AUTO INCREMENT, NOT NULL files_name => VARCHAR(255), NOT NULL (some other fields such as file type etc) TABLE File_Permissions perm_id => INT(12), PRIMARY, AUTO INCREMENT, NOT NULL perm_files_id => INT(12), NOT NULL perm_users_id => INT(12), NOT NULL
I retrieve a list of files that the user can view with the following SQL:
SELECT files_name FROM Files WHERE files_id IN (SELECT perm_files_id FROM File_Permissions WHERE perm_users_id = 'xxxxxx');
As far as I can tell, this will go through each of the thousands of entries in the Files table, and for each of them a subquery is executed, which selects from the File_Permissions table to check for the user ID.
It takes almost 2 seconds for each request. Iβm sure that something is fundamentally wrong with this, I just donβt know what it is.
Thank you for help!
source share