Reputation: 141
I want to use wordpress to access CAD drawing files.
Each drawing contains multiple entities of different types: lines, text, dimensions, etc.
I am storing all entities together in a single table in the following way:
| DrawingID | entityID | entityType | colour | other |
The points on the line as follows:
| pointID | drawing | entity | x-coord | y-coord | z-coord |
To render the drawing, I obtain the drawing ID and then step through all the entities with that drawing ID.
"SELECT * FROM 'entities' WHERE 'DrawingID'=57;"
If the entitytype is a LINE (for example), then I step through all the points on that line getting the coordinates of each point to render the line on the canvas.
"SELECT * FROM 'points' WHERE 'DrawingID'=57 AND 'entity'=457885;"
My problem is that the user will probably want to zoom in and out of the drawing requiring many queries through the database and I'm thinking it would be better to split out all entities of the drawing into a separate temporary database so that at least the drawingID query could be removed.
Is there a mechanism for doing this? Is there a better, more efficient, way of doing what I'm trying to accomplish?
Upvotes: 0
Views: 52
Reputation: 142298
This 'composite' index will help performance for that query:
INDEX(DrawingID, entity) -- in either order
This should make the query so fast that you should not even consider a "separate database...".
Is it entitiy
or entityID
??
Upvotes: 1
Reputation: 3055
Building and querying a new separate temporary database will not result in a huge improvement of performance.
If you want to use the database, to get an improvement you could start using a prepared statement, which is a special statement that the database parses and compiles, optimizing the SQL statement and storing the result in a template.
When you bind the the values to the parameters, the database executes the statement.
Assuming you're storing the entityID into $entityID, you could run:
$stmt = $conn->prepare("SELECT * FROM 'points' WHERE 'DrawingID'=57 AND 'entity'=?");
$stmt->bind_param("i", $entityID);
However I'd look to a stronger solution caching the results either on the server or the client. There are plenty of ways to cache results, but to summarize, you could export all the points of your drawing to a JSON file (XML is another option) and render the points with a library.
You should also give a look at the PHP documentation for Prepared Statement that has a lot of examples and explain benefits and caveats of using prepared statements.
However the best solution for I can think right now, is not to use the database but to export your CAD drawing into an SVG. Then all the zooming can be done on client side, with almost no overhead for your server.
Upvotes: 1