I’m a student working on a module for moodle cms (course management system) of my college. I have to write some join queries for my module. I can not make changes to table structures, they are pretty much set in stone (I didn’t make them, they were given to me).
I have no experience with writing queries for large databases. I’ve created a working prototype of my module and now I’m trying to organize the code/optimize queries etc.
Tasks:
| id | task | -------------------- | 1 | task1 | | 2 | task3 | | 3 | task3 | | 4 | task4 | | ... | ... |
Assets:
| id | asset | -------------------- | 1 | task1 | | 2 | task3 | | 3 | task3 | | 4 | task4 | | ... | ... |
TaskAsset:
| id | taskid | assetid | coefficient | ----------------------------------------------- | 1 | 2 | 33 | coefficient1 | | 2 | 5 | 35 | coefficient2 | | 3 | 6 | 36 | coefficient3 | | 4 | 8 | 37 | coefficient4 | | 5 | ... | ... | ... |
$query = "SELECT TaskAsset.id as id, Assets.asset AS asset, Tasks.task AS task
, coefficient
FROM Tasks, Assets, Taskasset
WHERE Taskasset.taskid= Tasks.id AND TaskAsset.assetid = Assets.id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']." - ".$row['asset']." - ".$row['task'] . $row['coefficient'];
echo "<br />";
}
Questions:
1.) So, if table structures are like these, is my query effective?
If they are, is a simple join still effective if I have to join more tables? Like 4 or 5?
2.) How do I rate effectiveness of queries? In phpmyadmin, I can see the time it took for the query to run. I’ve never used anything else for this because my tables had very few records, so it did not matter.
The only thing that I would do differently is explicitly specify the joins.
This does the same thing but I personally prefer it a lot better. That said, you should try to run an
EXPLAINon your query. That is where you’ll see the pressure points.