简体   繁体   English

临时数据库以减少MariaDB中的查询

[英]Temporary database to reduce queries in MariaDB

I want to use wordpress to access CAD drawing files. 我想使用wordpress访问CAD工程图文件。

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. 要渲染图形,我获取图形ID,然后逐步浏览具有该图形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. 例如,如果entitytype是LINE,那么我逐步浏览该线上的所有点,以获取每个点的坐标以在画布上绘制该线。

"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. 我的问题是,用户可能需要通过数据库来放大和缩小需要很多查询的图形,并且我认为最好将图形的所有实体拆分到一个单独的临时数据库中,这样至少drawingID查询可以删除。

Is there a mechanism for doing this? 有这样做的机制吗? Is there a better, more efficient, way of doing what I'm trying to accomplish? 有没有更好,更有效的方法来完成我要完成的工作?

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. 如果要使用数据库,则可以使用预处理语句来进行改进,这是数据库解析和编译,优化SQL语句并将结果存储在模板中的特殊语句。

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: 假设您将entityID存储到$ entityID中,则可以运行:

$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. 有很多方法可以缓存结果,但总而言之,您可以将图形的所有点导出到JSON文件(XML是另一种选择)并使用库来渲染这些点。

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. 您还应该看一下有关Prepared StatementPHP文档,其中包含许多示例,并说明了使用Prepared Statement的好处和注意事项。

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. 但是,我现在能想到的最佳解决方案是,不使用数据库,而是将CAD工程图导出到SVG中。 Then all the zooming can be done on client side, with almost no overhead for your server. 然后,所有缩放都可以在客户端完成,而服务器几乎没有开销。

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 ?? 它是entitiyentityID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM