0

我正在构建一个基于 Web 的商店应用程序,并且我必须处理彼此之间的许多嵌套子类别。关键是,我不知道我的脚本是否会处理数千个(新系统将替换旧系统,所以我知道我必须期待什么流量) - 目前,来自本地服务器的响应延迟为 1-2秒数比其他页面增加了大约 30 种不同类别的产品。

我的代码如下:

    BazaArkadiaDataContext db = new BazaArkadiaDataContext();
    List<A_Kategorie> Podkategorie = new List<A_Kategorie>();

    public int IdKat { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<A_Produkty> Produkty = new List<A_Produkty>(); //list of all products within the category and remaining subcategories

            if (Page.RouteData.Values["IdKategorii"] != null)
            {
                string tmpkat = Page.RouteData.Values["IdKategorii"].ToString();
                int index = tmpkat.IndexOf("-");
                if (index > 0)
                    tmpkat = tmpkat.Substring(0, index);
                IdKat = db.A_Kategories.Where(k => k.ID == Convert.ToInt32(tmpkat)).Select(k => k.IDAllegro).FirstOrDefault();
            }
            else
                return;

            PobierzPodkategorie(IdKat);

            foreach (var item in Podkategorie)
            {
                var x = db.A_Produkties.Where(k => k.IDKategorii == item.ID);
                foreach (var itemm in x)
                {
                    Produkty.Add(itemm);
                }
            }

            //data binding here
        }
    }

    List<A_Kategorie> PobierzPodkategorie(int IdKat, List<A_Kategorie> kat = null)
    {
        List<A_Kategorie> Kategorie = new List<A_Kategorie>();
        if (kat != null)
            Kategorie.Concat(kat);

        Kategorie = db.A_Kategories.Where(k => k.KatNadrzedna == IdKat).ToList();

        if (Kategorie.Count() > 0)
        {
            foreach (var item in Kategorie)
            {
                PobierzPodkategorie(item.IDAllegro, Kategorie);
                Podkategorie.Add(item);
            }
        }
        return Kategorie;
    }

TMC;博士*

我的函数PobierzPodkategorie递归地搜索子类别(子类别得到KatNadrzedna其父类别的列,位于 中IDAllegro),选择具有子类别 ID 的所有产品并将其添加到Produkty列表中。数据库结构非常糟糕,因为类别列表是从另一个商店服务服务器下载的,它需要获取我们自己的 ID 列,以防外国服务器改变结构。

类别列表中有超过 30 000 个条目,其中一些将有 5 个或更多父项,并且网站将仅显示主要类别和子类别(与 SOAP 连接的外部商店需要“较低”子类别)。

我的问题是

将索引表添加到数据库(类别123是父级123412738...)会提高性能,还是只是浪费时间?(索引应该在 API 版本更改时更新,我不知道它会多久更新一次)或者有其他方法吗?

我问是因为在生产中无法更改脚本,而且我不知道数据库引擎如何处理大量请求 - 我非常感谢任何帮助。

数据库是 MSSQL


*代码太多;没读

4

2 回答 2

1

You definitely should move the recursion into database. It can be done using WITH statement and Common Table Expressions. Then create a view or stored procedure and map it to you application.

With that you should be able to reduce SQL queries to two (or even one).

于 2013-03-17T12:41:28.267 回答
1

The big efficiency gain you can get is to load all subproducts in a single query. The time saved by reducing network trips can be huge. If 1 is a root category and 12 a child category, you can query all root categories and their children like:

select  *
from    Categories
where   len(Category) <= 2

An index on Category would not help with the above query. But it's good practice to have a primary key on any table. So I'd make Category the primary key. A primary key is unique, preventing duplicates, and it is indexed automatically.

Moving away from RBAR (row by agonizing row) has more effect than proper tuning of the database. So I'd tackle that first.

于 2013-03-17T12:41:59.493 回答