C#——数据库真分页查询与假分页查询

发布时间 2023-12-18 09:22:31作者: 高小浩upup

使用建议:

    如果数据量较小或者需要高效地查询特定数据,则应该使用真分页查询;如果数据量较大或者需要快速展示数据,则可以考虑使用假分页查询。

真分页:

    真分页查询是指将数据从数据库中按照每页固定的记录数进行分页,例如每页显示10条记录。在查询语句中通过使用 OFFSET 和 FETCH NEXT 语句来实现分页,OFFSET 表示偏移量,FETCH NEXT 表示获取下一批数据。这种方式可以准确地控制分页的位置和数量,但对于大数据量的查询会比较慢。

真分页查询示例:

以下是C#中实现真分页查询的示例代码:

public List<Customer> GetCustomers(int pageNumber, int pageSize)
{
    using (var context = new MyDbContext())
    {
        var customers = context.Customers.OrderBy(c => c.Name)
                                          .Skip((pageNumber - 1) * pageSize)
                                          .Take(pageSize)
                                          .ToList();
        return customers;
    }
}

在上述示例代码中,我们使用Entity Framework Core(EF Core)进行数据库操作。首先,通过OrderBy方法对数据进行排序;然后,使用Skip和Take方法实现分页,Skip表示跳过前面几条记录,Take表示获取接下来的几条记录。

假分页:

    假分页查询则是先查询出所有数据,再通过前端实现分页。例如,在前端中显示10条记录,当用户点击“下一页”时,再将接下来的10条数据显示出来。这种方式可以减轻数据库的压力,但在数据量极大的情况下,会对前端性能造成影响。

假分页查询示例:

以下是C#中实现假分页查询的示例代码:

public List<Customer> GetCustomers(int pageNumber, int pageSize)
{
    using (var context = new MyDbContext())
    {
        var customers = context.Customers.ToList();
        var pagedCustomers = customers.Skip((pageNumber - 1) * pageSize)
                                      .Take(pageSize)
                                      .ToList();
        return pagedCustomers;
    }
}

在上述示例代码中,我们首先通过ToList方法将所有记录查询出来,然后再使用Skip和Take方法实现分页,返回分页后的结果。需要注意的是,这种方式会将所有数据加载到内存中,因此如果数据量较大,则可能会导致性能问题。

在前端实现假分页查询时,需要先获取所有的数据,然后通过JavaScript或其他前端框架来进行分页展示。以下是一个简单的前端假分页查询的示例代码:

<!DOCTYPE html>
<html>
<head>
    <title>假分页查询示例</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(customer, index) in displayedCustomers" :key="index">
                    <td>{{ customer.id }}</td>
                    <td>{{ customer.name }}</td>
                    <td>{{ customer.email }}</td>
                </tr>
            </tbody>
        </table>

        <div>
            <button @click="previousPage">上一页</button>
            <span>{{ currentPage }}</span>
            <button @click="nextPage">下一页</button>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                customers: [], // 所有数据
                displayedCustomers: [], // 当前页面展示的数据
                pageSize: 10, // 每页显示的记录数
                currentPage: 1 // 当前页码
            },
            created() {
                // 在这里可以通过接口或其他方式获取所有数据
                this.customers = [
                    { id: 1, name: 'Customer 1', email: 'customer1@example.com' },
                    { id: 2, name: 'Customer 2', email: 'customer2@example.com' },
                    // ...
                ];

                this.updateDisplayedCustomers();
            },
            methods: {
                updateDisplayedCustomers() {
                    const startIndex = (this.currentPage - 1) * this.pageSize;
                    const endIndex = startIndex + this.pageSize;
                    this.displayedCustomers = this.customers.slice(startIndex, endIndex);
                },
                previousPage() {
                    if (this.currentPage > 1) {
                        this.currentPage--;
                        this.updateDisplayedCustomers();
                    }
                },
                nextPage() {
                    const totalPages = Math.ceil(this.customers.length / this.pageSize);
                    if (this.currentPage < totalPages) {
                        this.currentPage++;
                        this.updateDisplayedCustomers();
                    }
                }
            }
        });
    </script>
</body>
</html>

在上述示例代码中,我们使用了Vue.js作为前端框架。首先,在Vue的created生命周期钩子中获取所有的数据,并将其保存在customers数组中。然后,通过计算属性displayedCustomers来获取当前页要展示的数据。updateDisplayedCustomers方法根据当前页码和每页显示的记录数,使用slice方法从customers数组中截取对应页码的数据。最后,在按钮的点击事件中更新当前页码,并调用updateDisplayedCustomers方法来更新展示的数据。

需要注意的是,假分页查询在前端会加载所有的数据,因此如果数据量较大,可能会影响页面加载的性能。