I am now developing module that generate data as following structure into json from mysql.
{"employee":
[
{"id":1,
"name":"jhon doe",
"register_date":"2011-05-11",
"education":
[
{"degree":"B.A","description":"History"},
{"degree":"M.A","description":"History"}
]
},
{"id":2,
"name":"Smith",
"register_date":"2011-06-11",
"education":
[
{"degree":"B.E","description":"Mechnical"},
{"degree":"M.E","description":"Mechnical"}
]
}
]
}
To achieve this, firstly, I retrieve employee data by register_date range as follow.
$result=mysql_query("SELECT * FROM employee WHERE register_date>'2011-04-31' AND register_date<'2011-07-01'");
Then I iterate each row from result and retrieve education information from education table as follow:
while($row = mysql_fetch_assoc($result)){
$id=$row["id"];
$education=mysql_query("SELECT degree,description from education where emp_id=$id");
// assigning education array as educaiton field in $row
// write json_encode($row) to output buffer
}
This project's data structure is not my design and I know it's not a good idea setting employee'id as foreign key in education table, instead, It should be set education id as foreign key in employee table. My problem is that (by using this data structure) retrieving education list for each row of employee is huge performance issue because there may be about 500000 record of employee for a month and for that amount, mysql select queries have to be processed 500000 times for education data retrieval for each employee. how should I optimize.
1.Should I change data structure?
2. Should I create mysql stored procedure that generate json string directly from database?
3. Should I denormalize education data in employee table?
which is most efficient approach or any suggestion?
Please help me.
mysql_queryin 2015.)