ASP.NET MVC 视图中如何处理 IQueryables?

发布于 2024-11-18 18:01:53 字数 1198 浏览 4 评论 0原文

我在 MySQL 数据库中有一些表来表示来自传感器的记录。我正在开发的系统的功能之一是将数据库中的记录显示给Web用户,因此我使用ADO.NET实体数据模型创建ORM,使用Linq to SQL从数据库获取数据,并将它们存储在我设计的 ViewModel 中,以便我可以使用 MVCContrib Grid Helper 显示它:

public IQueryable<TrendSignalRecord> GetTrends()
{
    var dataContext = new SmgerEntities();
    var trendSignalRecords = from e in dataContext.TrendSignalRecords
                select e;

    return trendSignalRecords;
}

public IQueryable<TrendRecordViewModel> GetTrendsProjected()
{
    var projectedTrendRecords = from t in GetTrends()
    select new TrendRecordViewModel
    {
        TrendID = t.ID,
    TrendName = t.TrendSignalSetting.Name,
    GeneratingUnitID = t.TrendSignalSetting.TrendSetting.GeneratingUnit_ID,
    //{...}
    Unit = t.TrendSignalSetting.Unit
    };
    return projectedTrendRecords;
}

我调用 GetTrendsProjectedMethod,然后使用 Linq to SQL 仅选择我想要的记录。它在我的开发场景中工作正常,但是当我在真实场景中测试它时,记录数量要大得多(大约一百万条记录),它会停止工作。

我放置了一些调试消息来测试它,一切正常,但是当它到达 return View() 语句时,它只是停止,向我抛出 MySQLException: Timeout expired 。这让我想知道我发送到页面的数据是否由页面本身检索(它仅在页面本身需要时搜索数据库中显示的项目,或类似的东西)。

我的所有其他页面都使用同一组工具:MVCContrib Grid Helper、ADO.NET、Linq to SQL、MySQL,其他一切都正常。

I have some tables in a MySQL database to represent records from a sensor. One of the features of the system I'm developing is to display this records from the database to the web user, so I used ADO.NET Entity Data Model to create an ORM, used Linq to SQL to get the data from the database, and stored them in a ViewModel I designed, so I can display it using MVCContrib Grid Helper:

public IQueryable<TrendSignalRecord> GetTrends()
{
    var dataContext = new SmgerEntities();
    var trendSignalRecords = from e in dataContext.TrendSignalRecords
                select e;

    return trendSignalRecords;
}

public IQueryable<TrendRecordViewModel> GetTrendsProjected()
{
    var projectedTrendRecords = from t in GetTrends()
    select new TrendRecordViewModel
    {
        TrendID = t.ID,
    TrendName = t.TrendSignalSetting.Name,
    GeneratingUnitID = t.TrendSignalSetting.TrendSetting.GeneratingUnit_ID,
    //{...}
    Unit = t.TrendSignalSetting.Unit
    };
    return projectedTrendRecords;
}

I call the GetTrendsProjectedMethod and then I use Linq to SQL to select only the records I want. It is working fine in my developing scenario, but when I test it in a real scenario, where the number of records is way greater (something around a million records), it stops working.

I put some debug messages to test it, and everything works fine, but when it reaches the return View() statement, it simply stops, throwing me a MySQLException: Timeout expired. That let me wondering if the data I sent to the page is retrieved by the page itself (it only search for the displayed items in the database when the page itself needs it, or something like that).

All of my other pages use the same set of tools: MVCContrib Grid Helper, ADO.NET, Linq to SQL, MySQL, and everything else works alright.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-11-25 18:01:53

如果您有数百万条记录,您绝对应该在执行查询之前对数据集进行分页。这可以使用 .Skip.Take 扩展方法来完成。在对数据库运行任何查询之前应该调用它们。

尝试在不分页的情况下从数据库中获取数百万条记录很可能会导致超时。

You absolutely should paginate your data set before executing your query if you have millions of records. This could be done using the .Skip and .Take extension methods. And those should be called before running any query against your database.

Trying to fetch millions of records from a database without pagination would very likely cause a timeout at best.

伤感在游骋 2024-11-25 18:01:53

好吧,假设 此博客是正确的,.AsPagination 方法要求您按特定列对数据进行排序。尝试对包含数百万条记录的表执行 OrderBy 可能只是一项耗时的操作,并且会超时。

Well, assuming information in this blog is correct, .AsPagination method requires you to sort your data by a particular column. It's possible that trying to do an OrderBy on a table with millions of records in it is just a time consuming operation and times out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文