Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
imi
imi
提交
90935273
imi
项目概览
imi
/
imi
10 个月 前同步成功
通知
53
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
imi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
90935273
编写于
6月 21, 2023
作者:
weixin_47267244
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
支持 MySQL 高性能分页查询(大表分页类)
上级
90b1f283
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
113 addition
and
0 deletion
+113
-0
doc/components/db/index.md
doc/components/db/index.md
+24
-0
src/Db/Mysql/Query/Pagination/BigTablePagination.php
src/Db/Mysql/Query/Pagination/BigTablePagination.php
+57
-0
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
+32
-0
未找到文件。
doc/components/db/index.md
浏览文件 @
90935273
...
...
@@ -722,6 +722,30 @@ var_dump(json_encode($data)); // 支持序列化
]
```
### 高性能分页查询
```
php
// 首先准备好查询构建器
$query
=
Db
::
query
()
->
from
(
'xxxtable'
);
// 实例化大表分页类,字段名默认 id
$pagination
=
new
\
Imi\Db\Mysql\Query\Pagination\BigTablePagination
(
$query
);
// 指定主表主键字段名,如果涉及多张表关联,这里需要指定主表的主键字段名
$pagination
=
new
\
Imi\Db\Mysql\Query\Pagination\BigTablePagination
(
$query
,
'xxxtable.id'
);
// 只查列表,返回值同 $query->select(),Result 对象
$result
=
$pagination
->
select
();
// 获取数组
$result
->
getArray
();
// 分页查询,返回值同 $query->paginate(),PaginationResult 对象
$page
=
1
;
$limit
=
10
;
$result
=
$pagination
->
paginate
(
$page
,
$limit
);
```
> 仅 MySQL 数据库支持
## 查询执行
### 查询记录
...
...
src/Db/Mysql/Query/Pagination/BigTablePagination.php
0 → 100644
浏览文件 @
90935273
<?php
declare
(
strict_types
=
1
);
namespace
Imi\Db\Mysql\Query\Pagination
;
use
Imi\Db\Query\Interfaces\IPaginateResult
;
use
Imi\Db\Query\Interfaces\IQuery
;
use
Imi\Db\Query\Interfaces\IResult
;
use
Imi\Db\Query\PaginateResult
;
use
Imi\Util\Pagination
;
class
BigTablePagination
{
protected
IQuery
$query
;
protected
string
$idField
=
''
;
public
function
__construct
(
IQuery
$query
,
string
$idField
=
'id'
)
{
$this
->
query
=
$query
;
$this
->
idField
=
$idField
;
}
public
function
getQuery
():
IQuery
{
return
$this
->
query
;
}
public
function
paginate
(
int
$page
,
int
$limit
,
array
$options
=
[]):
IPaginateResult
{
if
(
$options
[
'total'
]
??
true
)
{
$query
=
clone
$this
->
query
;
$option
=
$query
->
getOption
();
$option
->
order
=
[];
$total
=
(
int
)
$query
->
count
();
}
else
{
$total
=
null
;
}
$pagination
=
new
Pagination
(
$page
,
$limit
);
$query
=
clone
$this
->
query
;
return
new
PaginateResult
(
$this
->
select
(
$page
,
$limit
),
$pagination
->
getLimitOffset
(),
$limit
,
$total
,
null
===
$total
?
null
:
$pagination
->
calcPageCount
(
$total
),
$options
);
}
public
function
select
(
int
$page
,
int
$limit
):
IResult
{
$query
=
clone
$this
->
query
;
$ids
=
$query
->
field
(
$this
->
idField
)
->
page
(
$page
,
$limit
)
->
select
()
->
getColumn
();
$query
=
clone
$this
->
query
;
return
$query
->
whereIn
(
$this
->
idField
,
$ids
)
->
select
();
}
}
tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
浏览文件 @
90935273
...
...
@@ -8,6 +8,7 @@ use Imi\Db\Db;
use
Imi\Db\Mysql\Query\FullText\MysqlFullTextOptions
;
use
Imi\Db\Mysql\Query\FullText\SearchModifier
;
use
Imi\Db\Mysql\Query\Lock\MysqlLock
;
use
Imi\Db\Mysql\Query\Pagination\BigTablePagination
;
use
Imi\Db\Query\Database
;
use
Imi\Db\Query\Raw
;
use
Imi\Db\Query\Where\Where
;
...
...
@@ -187,6 +188,37 @@ abstract class QueryCurdBaseTest extends BaseTest
$this
->
assertEquals
(
$expectedData
[
'page_count'
],
$result
->
getPageCount
());
}
/**
* @depends testInsert
*/
public
function
testBigTablePagination
(
array
$args
):
void
{
[
'ids'
=>
$ids
]
=
$args
;
$expectedData
=
[
'list'
=>
[
[
'id'
=>
$ids
[
1
],
'title'
=>
'title-insert'
,
'content'
=>
'content-insert'
,
'time'
=>
'2019-06-21 00:00:00'
,
'member_id'
=>
0
,
],
],
'limit'
=>
1
,
'total'
=>
2
,
'page_count'
=>
2
,
];
$query
=
Db
::
query
(
$this
->
poolName
)
->
from
(
$this
->
tableArticle
);
$pagination
=
new
BigTablePagination
(
$query
);
$this
->
assertEquals
(
$expectedData
,
$pagination
->
select
(
2
,
1
)
->
getArray
());
$result
=
$pagination
->
paginate
(
2
,
1
);
$this
->
assertEquals
(
$expectedData
,
$result
->
toArray
());
$this
->
assertEquals
(
$expectedData
[
'list'
],
$result
->
getList
());
$this
->
assertEquals
(
$expectedData
[
'total'
],
$result
->
getTotal
());
$this
->
assertEquals
(
$expectedData
[
'limit'
],
$result
->
getLimit
());
$this
->
assertEquals
(
$expectedData
[
'page_count'
],
$result
->
getPageCount
());
}
/**
* @depends testInsert
*
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录