简体   繁体   English

在关系数据库中存储矩阵

[英]Storing matrices in a relational database

I am working on a project for a client and going through the initial database design.我正在为客户开展一个项目并完成初始数据库设计。 The project will be a simple web app for tracking processes and their outcomes within a matrix diagram, I am looking for a good way to store these in relational tables.该项目将是一个简单的网络应用程序,用于在矩阵图中跟踪过程及其结果,我正在寻找一种将这些存储在关系表中的好方法。

Right now I am thinking I have a general table for Routines which the x and y coords will map too and maybe off from that a lookup table containing the ID of coordinates in which a "hit" is recorded.现在我想我有一个通用的例程表,x 和 y 坐标也将映射,并且可能从包含记录“命中”的坐标 ID 的查找表中删除。 Anyone have any better ways of doing this?任何人都有更好的方法来做到这一点?

Thanks!谢谢!

EDIT:编辑:

This is just the beginning of the project so I have limited detail as of yet, but my main reasoning behind multiple tables is because the matrices will be completely dynamic in size and generic so that each one may be different and they will be tied to a user这只是项目的开始,所以我的细节还有限,但我在多个表背后的主要原因是因为矩阵的大小和通用性将是完全动态的,因此每个矩阵可能不同,并且它们将绑定到一个用户

I also forgot to mention that order of the x/y values are important, which further supported my reasoning behind having multiple tables for xy and values, from this I strongly assume that needing to know each individual cell is important我还忘了提到 x/y 值的顺序很重要,这进一步支持了我有多个表用于 xy 和值的推理,由此我强烈假设需要知道每个单独的单元格很重要

EXAMPLE:例子:

The basic example (albeit abstract) of this lies in the process regarding a restaurant.这方面的基本示例(尽管是抽象的)在于有关餐厅的流程。 The actions being stuff along the lines of sit down, order food, look over menu, order drinks, eat, pay, etc. the outcomes being order taken, drinks delivered, food delivered, change given.动作是坐下、点菜、查看菜单、点饮料、吃饭、付款等。结果是点单、送饮料、送食物、给零钱。 While seemingly simple it becomes complex when taken into consideration things happen differently with each occurrence, also in the case of take out or buffets.虽然看起来很简单,但考虑到每次发生的事情都不同,但在外卖或自助餐的情况下也会变得复杂。 the order of the actions and outcomes becomes integral in seeing the differences between the situations行动和结果的顺序在看到情况之间的差异时变得不可或缺

There are lots of way to do this, we would need a lot more information to be more specific about what would be best for you.有很多方法可以做到这一点,我们需要更多信息才能更具体地了解什么对您最有利。 However, here are the two SOP ways:但是,这里有两种 SOP 方式:

Either a separate table for each matrix:每个矩阵都有一个单独的表:

CREATE TABLE YourMatrixName(
    RowNo smallint NOT NULL,
    ColNo smallint NOT NULL,
    CellValue varchar](50) NULL,
 CONSTRAINT [PK_Matrices] PRIMARY KEY CLUSTERED 
    ([RowNo] ASC, [ColNo] ASC)
) ON [PRIMARY];
GO

CREATE UNIQUE NONCLUSTERED INDEX IX_YourMatrixName ON dbo.YourMatrixName
    (ColNo, RowNo); 
GO

Or, all of the matrices in one table:或者,一张表中的所有矩阵:

CREATE TABLE Matrices(
    MatrixName varchar(24) NOT NULL,
    RowNo smallint NOT NULL,
    ColNo smallint NOT NULL,
    CellValue varchar(50) NULL,
 CONSTRAINT [PK_Matrices] PRIMARY KEY CLUSTERED 
    ([MatrixName] ASC, [RowNo] ASC, [ColNo] ASC)
) ON [PRIMARY];
GO

CREATE UNIQUE NONCLUSTERED INDEX IX_Matrices ON dbo.Matrices
    (ColNo, RowNo); 
GO

These are standard normal form, virtually all other ways of doing it are not well normalized.这些是标准范式,实际上所有其他方法都没有很好地标准化。 Some advantages of these approaches:这些方法的一些优点:

  1. You do not have to fill in every cell, only the ones you are using.您不必填写每个单元格,只需填写您正在使用的单元格即可。 Or have a default value (0 or "") and skip those.或者有一个默认值(0 或“”)并跳过那些。
  2. This is easily the most flexible approach, even in the "all in one" model, there is no need to restrict them to the same size in any way, and it is very easy to resize them.这很容易成为最灵活的方法,即使在“多合一”模型中,也不需要以任何方式将它们限制为相同的大小,并且很容易调整它们的大小。
  3. You can easily query the contents of the matrix, something that is increasingly difficult in more compact storage methods.您可以轻松查询矩阵的内容,这在更紧凑的存储方法中变得越来越困难。
  4. "Hit"s or any other aspect of the matrix cells are easy to implement as additional fields in the rows. “命中”或矩阵单元格的任何其他方面很容易实现为行中的附加字段。 Make them Null-able if you're worried about the additional space, and index them if you want to query/report on these attributes separately.如果您担心额外的空间,请将它们设为 Null,如果您想分别查询/报告这些属性,请将它们编入索引。 Its also just as easy to retrofit features like this with this model also.使用此模型也同样容易改装这样的功能。

The primary disadvantage is that there is typically a high space to data overhead.主要缺点是数据开销通常有很高的空间。 Many assume that there is also high overhead to Insert or retrieve new matrices but in fact there are several documented techniques that can make it quite fast.许多人认为插入或检索新矩阵的开销也很高,但实际上有几种记录在案的技术可以使其变得非常快。

Video memory, a very simple 2D matrix is stored as follows:显存,一个很简单的二维矩阵存储如下:

ABCD
EFGH
IJKL

in ram sequentially like an array as在 ram 中像数组一样按顺序排列

A,B,C,D,E,F,G,H,I,J,K,L

element x,y can be found at array offset元素 x,y 可以在数组偏移处找到

[y*width+x]

for instance, x=2,y=2 (zero-based) refers to element K.例如,x=2,y=2(从零开始)指的是元素 K。

[y*width+x]=[2*4+2]=10. array element 10 (again zero-based) = K, so you're good.数组元素 10(再次从零开始)= K,所以你很好。

Storing in a comma-delimited list will let you put a matrix of any size in an nvarchar field.存储在逗号分隔的列表中可以让您在 nvarchar 字段中放置任意大小的矩阵。 This assumes that you don't need to query individual cells in SQL , but just grab the matrix as a whole and process it client-side.假设您不需要在 SQL 中查询单个单元格,而只需将矩阵作为一个整体抓取并在客户端进行处理。

Your table may look like this:您的表可能如下所示:

tbl_matrices
----
id
user_id
matrix nvarchar(max)

Is your matrix dense of sparse?你的矩阵密集稀疏吗? If it's sparse, it may be better for each entry to just store a list of hit's, rather than have a full 2D table that is mostly 0's.如果它很稀疏,那么每个条目最好只存储一个命中列表,而不是一个主要为 0 的完整 2D 表。

Rather than two tables, I would just use one table: (x, y, outcome).而不是两张表,我只会使用一张表:(x,y,结果)。 Beyond that it's hard to give any more advice with the limited information given.除此之外,以有限的信息很难给出更多的建议。

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

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