从数据库填充许多控件(架构和性能问题)

发布于 2024-11-27 23:29:27 字数 294 浏览 1 评论 0原文

所以我有这个页面,它收集了大量信息来创建一个稍微复杂的对象。所以我将所有相关详细信息收集在一个 div 中,大约有 7 个 Div,并且填充了大约 20 个控件(DropDownListss、CheckBoxLists、Repeaters 等)。

现在我只是创建在 Click 和 Page_Load 事件上调用的方法。 每个填充过程都是单独完成的。

代码很麻烦,而且我也不认为性能很好,因为我知道大量的数据库连接/行程对性能和数据库负载不利。

那么我怎样才能把这个功能放在更好的设计和更好的性能中,这里有什么帮助吗 =) ?

So I have this page that collections a lot of information to create a little complicated object. so I collect all the related details together in a div and I have like 7 Divs, and I populate like 20 controls (DropDownListss, CheckBoxLists, Repeaters, etc).

Now I'm just creating methods to be called on Click and Page_Load events.
each population process is done separately.

The code is a hassle and I don't except the performance to be good either, because I know that a lot of connections/trips to the database is not good for performance and the load on the database.

so how can I put this functionality in a better design and a better performance, any help here =) ?

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

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

发布评论

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

评论(4

诗酒趁年少 2024-12-04 23:29:27

为什么不在 Page_Load 中加载所有这些数据?然后一旦进入内存(无论是在对象、数据读取器还是数据集中),填充您的控件?

Why not load all this data in Page_Load? Then once in memory (whether in objects, data readers, or data sets), populate your controls?

甜点 2024-12-04 23:29:27

这是一个有点哲学的问题。就页面性能而言。如果只有 20 个控件,您不应该看到问题。但是,您有两个关键瓶颈:

1) 从数据库获取
您需要尝试立即从数据库中获取所有数据。当然,这并不总是可行,但它会为您节省几次往返时间。您还可以为当前请求缓存该对象。

2) 保存
如果您在回发时将每个更改事件写入数据库,则将花费不必要的长时间。我建议您创建一个 Model 属性,该属性从数据库获取模型,并且每个更改处理程序都会更改该模型。一旦回发全部触发(我现在不记得最好的事件,但我认为 Page_Load 没问题),只需一次提交即可将该模型写入数据库。

就性能而言,这可能是获得最大收益的最少努力。您还可以使用 Ajax 加载控件以使其看起来更快,但这并不能解决任何问题。

That's a slightly philosophical question. As far as page perf. goes you shouldn't see a problem with only 20 controls. However, you have two key bottlenecks:

1) Get from database
You need to try and get all of the data from the database at once. Of course that's not always possible but it will save you a couple roundtrips. You can also cache that object for the current request.

2) Save
if you write to db on each change event on postback, it will take unnecessarily long. I'd suggest you crete a Model property that gets the model from the db and each change handler changes that model. Once the postbacks have all fired (I can't remember the best event now, but I think Page_Load is ok) write that model to the database in one single commit.

As far as performance goes, this is probably the least effort for maximum gain. You can also load controls with Ajax to make seem faster but it doesn't solve anything.

幻梦 2024-12-04 23:29:27

NHibernate 有一个非常好的功能,称为 NHibernate Futures (但并非所有数据库都受支持)。您还可以从中获得其他好处,例如开箱即用的缓存、批量写入查询等。所以我建议考虑这个选项。

顺便说一句,您真的需要在屏幕上同时显示所有这些信息吗?例如,您可以拆分 UI 层并创建向导。

NHibernate has very nice feature called NHibernate Futures (but not all databases are supported). You could also get another benefits from it such as caching out of the box, batching write queries and so on. So I recommend to consider this option.

BTW, do you really need all this information at the same time on the screen? You could split your UI layer and create, for example, wizard.

素年丶 2024-12-04 23:29:27

如果您自己编写 SQL 查询,则可以组合大多数查询,这样每个表只需要一个查询。

例如,您可能有表 dropdown_choices,其中包含 dropdown_idchoice 列。

您可以选择所有与您有关的数据,然后在代码中将其拆分。

SELECT * FROM dropdown_choices WHERE dropdown_id IN (1, 2, 3, 10, ...);

If you write the SQL queries yourself, you can combine most queries so that you only need one query per table.

E.g. you might have table dropdown_choices, which has dropdown_id and choice columns.

You could SELECT all the data that concerns you and then split it up in your code.

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