Our data is confidential thus I am creating dummy data here. The number of records is 100 000.
Table1 Form:
+------------------+
| formID formName |
+------------------+
| 1 student |
| 2 teacher |
+------------------+
Table2 Field:
+--------------------+
| fieldID fieldName |
+--------------------+
| 1 Name |
| 2 Location |
+--------------------+
Table3 FormField:
+----------------------------+
| formID fieldID Value |
+----------------------------+
| 1 1 studentName |
| 1 2 ahmedabad |
| 2 1 teacherName |
| 2 2 mumbai |
+----------------------------+
My query:
select frm.formName,
(
select frmfld.Value from FormField frmfld
WHERE frmfld.fieldID = 1 AND frmfld.formID = frm.formID
) AS Name,
(
select frmfld.Value from FormField frmfld
WHERE frmfld.fieldID = 2 AND frmfld.formID = frm.formID
) AS Location
From Form frm
Here, fields may increase dynamically and for that field we are appending new subquery portion every time while creating query using codebase. As this fields grows to 100, execution time for this query grows to 7 to 8mins because of the subqueries and which is not acceptable.
EXPLAIN
, provide your MySQL configuration variables, provide your server hardware information. You are using an EAV structure, and judging by what you do - it most likely could have been designed better. Also, what is 1lac? I have never seen that, any chance you can convert that to metric units?JOIN
s will do.JOIN
s only. Are there any other requirements you didn't mention?