在更大的系统中加载数据的最佳选择是什么? (Laravel)
What are the best options for data loading in bigger systems? (Laravel)
正如我的问题所说,我想知道在 Laravel.
的更大系统中加载数据的最佳选择是什么
目前我使用 Laravel Eloquent 从数据库中提取数据,在我看来我使用 dataTables JS库。它对较小的系统或网站有效。然而,我似乎发现自己处于一个位置,即更大更复杂的系统需要很长时间才能加载这些数据(有时超过 15 秒)。
我找到了一些解决方案:
- 预加载关系有助于行
中的关系
- 使用 DB 代替 Eloquent
- 使用 Laravel 分页 而不是数据表分页
- 正在从 ajax 源
将数据加载到数据表
但是分页有一些问题,尤其是 dataTables 有 ordering/searching 数据选项。
我的问题是,对于如何以最有效的方式加载数据,您是否有任何建议,以便它尽可能快,代码尽可能干净。当您需要加载大量数据时,您会怎么做?
优化查询的最佳方法之一是使用 MySQL indexes。根据此文档:
Without an index, MySQL must begin with the first row and then read
through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns
in question, MySQL can quickly determine the position to seek to in
the middle of the data file without having to look at all the data.
This is much faster than reading every row sequentially.
创建索引最简单的方法如下:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
如果你想要Laravel创建索引的方式,你可以使用index()
方法来实现,按照官方documentation:
$table->index('column');
正如我的问题所说,我想知道在 Laravel.
的更大系统中加载数据的最佳选择是什么目前我使用 Laravel Eloquent 从数据库中提取数据,在我看来我使用 dataTables JS库。它对较小的系统或网站有效。然而,我似乎发现自己处于一个位置,即更大更复杂的系统需要很长时间才能加载这些数据(有时超过 15 秒)。
我找到了一些解决方案:
- 预加载关系有助于行 中的关系
- 使用 DB 代替 Eloquent
- 使用 Laravel 分页 而不是数据表分页
- 正在从 ajax 源 将数据加载到数据表
但是分页有一些问题,尤其是 dataTables 有 ordering/searching 数据选项。
我的问题是,对于如何以最有效的方式加载数据,您是否有任何建议,以便它尽可能快,代码尽可能干净。当您需要加载大量数据时,您会怎么做?
优化查询的最佳方法之一是使用 MySQL indexes。根据此文档:
Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. This is much faster than reading every row sequentially.
创建索引最简单的方法如下:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
如果你想要Laravel创建索引的方式,你可以使用index()
方法来实现,按照官方documentation:
$table->index('column');