简体   繁体   English

如何组织数据库中的数据?

[英]How to organize data from the database?

I have a MySQL database with tables for Items and for Stores , and a lookup table ItemStores , like this: 我有一个MySQL数据库,其中包含用于ItemsStores表,以及一个查找表ItemStores ,如下所示:

CREATE TABLE IF NOT EXISTS Items (
    itemId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    description VARCHAR(256) NOT NULL DEFAULT '',
    priceBeforeTax DECIMAL(10,2) NOT NULL,
) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXISTS Stores (
    storeId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    storeName VARCHAR(128) NOT NULL,
) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXISTS ItemStores (
    storeId INT NOT NULL REFERENCES Stores(storeId),
    itemId INT NOT NULL REFERENCES Items(itemId),
    quantity INT NOT NULL
) DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

The application stores the Items and Stores in ObservableCollections s of Item and Store object, and some WPF controls are bound to these ObservableCollections s, like so: 应用程序将Item和Stores存储在Item and Store对象的ObservableCollections中,并且某些WPF控件绑定到这些ObservableCollections ,如下所示:

public class Store
{
    public int storeId { get; set; }
    public string storeName { get; set; }

    public override string ToString()
    {
        return this.branchName;
    }
}
private static ObservableCollection<Store> _stores = new ObservableCollection<Store>();
public static ObservableCollection<Store> Stores
{
    get { return _stores; }
}


public class Item
{
    public int itemId { get; set; }
    public string description { get; set; }
    public decimal priceBeforeTax { get; set; }

    public override string ToString()
    {
        return this.description;
    }
}
private static ObservableCollection<Item> _items = new ObservableCollection<Item>();
public static ObservableCollection<Item> Items
{
    get { return _items; }
}

Now my dilemma centers on how to assign the ItemStores data. 现在,我的困境是如何分配ItemStores数据。 I am thinking about having a HashTable in each Item that maps storeID to the quantity of the item in each Store . 我正在考虑在每个Item中都有一个HashTable,以将storeID映射到每个Store中该项目的数量。 Is there a better way to map it, assuming that I will be occasionally searching for an Item by its itemId ? 假设我偶尔会通过itemId搜索某个Item ,是否有更好的映射方法? I'm already running a SELECT * FROM Items to populate the ObservableCollection, I fear that running another query to get the quantity from each store would really hammer MySQL. 我已经在运行SELECT * FROM Items来填充ObservableCollection,我担心运行另一个查询以从每个存储中获取数量确实会打击MySQL。 A JOIN won't help as I do not know how many stores will be returned and returning multiple rows for the same item (one row for each store that carries it) will make it a pain to put the data into the ObservableCollection. JOIN不会帮忙,因为我不知道将返回多少商店,并且对于同一项目返回多行(每个存储该行的商店将返回一行)会使将数据放入ObservableCollection变得很痛苦。

I would not take that road. 我不会走那条路。 You will soon find yourself building an ORM instead of using one. 您很快会发现自己正在构建ORM,而不是使用ORM。 Furhermore, reading whole database into memory collections is bad idea because it will become performance problem once you realize that your data is stale and start reloading everything. 此外,将整个数据库读取到内存集合中不是一个好主意,因为一旦您意识到数据过时并开始重新加载所有内容,它将成为性能问题。

As for original question, ORM classes have collections of related children as properties, so you will not have to search for all of them - you will find a Store and read all ItemStore objects. 对于原始问题,ORM类具有相关子项的集合作为属性,因此您不必搜索所有子项-您将找到一个Store并读取所有ItemStore对象。 If you need a list of an item in all stores, you will ask a database for that information. 如果需要所有商店中的商品清单,则将要求数据库提供该信息。

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

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