SQL Server ->> OFFSET & FETCH子句

本文详细探讨了SQLServer2012中OFFSET+FETCH字句的应用,通过实例展示了如何使用该特性从结果集中高效地获取特定行数,并分析了其性能表现与执行计划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL Server 2012引入OFFSET + FETCH字句。它俩出现在SELECT .... ORDER BY ...后面。作用是告诉SQL Server在结果集中忽略前N行然后取前M行出来。

比如

SELECT NUM
FROM dbo.Numbers
ORDER BY Num
OFFSET 100 ROWS
FETCH NEXT 5 ROWS ONLY

 

主要要观察下性能如何,执行计划。实际行数为105。没有出现不必要的行扫描。

转载于:https://www.cnblogs.com/jenrrychen/p/5165033.html

void PersonManagement::initUI() { setObjectName("PersonManagement"); m_personModel = new personModel(this, Database::GetInstance()->getDB()); m_personModel->setTable("personInfo"); m_personModel->select(); // 创建数据模型 m_personTable = new QTableView(this); m_personTable->setModel(m_personModel); QHeaderView* horizontalHeader = m_personTable->horizontalHeader(); // 设置除第五列外的所有列自由拉伸 for (int i = 0; i < m_personModel->columnCount(); ++i) { if (i != 5 || i != 6) { horizontalHeader->setSectionResizeMode(i, QHeaderView::Stretch); } } // 设置第五列固定宽度 horizontalHeader->setSectionResizeMode(5, QHeaderView::Fixed); m_personTable->setColumnWidth(5, 100); // 设置第六列固定宽度 horizontalHeader->setSectionResizeMode(6, QHeaderView::Fixed); m_personTable->setColumnWidth(6, 130); // 其他设置保持不变 m_personTable->verticalHeader()->setVisible(false); m_personTable->setEditTriggers(QAbstractItemView::NoEditTriggers); m_personTable->setSelectionBehavior(QAbstractItemView::SelectRows); m_personTable->hideColumn(0); m_personTable->verticalHeader()->setDefaultSectionSize(100); // 创建搜索框 m_searchLineEdit = new QLineEdit(this); m_searchLineEdit->setPlaceholderText("搜索人员..."); m_searchLineEdit->setMinimumWidth(250); // 使用 Qt 自带的搜索图标(兼容性版本) QIcon searchIcon = style()->standardIcon(QStyle::SP_FileDialogContentsView); QAction *searchAction = new QAction(searchIcon, "搜索", this); m_searchLineEdit->addAction(searchAction, QLineEdit::TrailingPosition); // 创建按钮 m_addPersonButton = new QPushButton("增加人员", this); m_addPersonButton->setFixedSize(95, 30); m_exportButton = new QPushButton("导出数据", this); m_exportButton->setFixedSize(95, 30); m_importButton = new QPushButton("导入数据", this); m_importButton->setFixedSize(95, 30); QPushButton *rename = new QPushButton("修改照片名", this); connect(rename, &QPushButton::clicked, this, &PersonManagement::onrename); // 按钮布局(添加间距和对齐) QHBoxLayout *buttonLayout = new QHBoxLayout(); buttonLayout->addWidget(m_searchLineEdit); // 搜索框居左 buttonLayout->addStretch(1); // 间距 buttonLayout->addWidget(m_addPersonButton); buttonLayout->addSpacing(10); // 按钮间距 buttonLayout->addWidget(m_exportButton); buttonLayout->addSpacing(10); buttonLayout->addWidget(m_importButton); buttonLayout->addWidget(rename); buttonLayout->setContentsMargins(10, 10, 10, 10); // 布局边距 //分页 m_upPageBtn = new QPushButton("上一页", this); m_upPageBtn->setFixedSize(80, 30); m_downPageBtn = new QPushButton("下一页", this); m_downPageBtn->setFixedSize(80, 30); m_jumpBtn = new QPushButton("跳转", this); m_jumpBtn->setFixedSize(80, 30); m_currentPage = new QLabel("当前 页", this); m_currentPage->setFixedSize(60, 30); //跳转输入页 m_jumpBox = new QSpinBox(this); m_jumpBox->setRange(1,999); m_jumpBox->setFixedSize(50, 30); QHBoxLayout *pageTurnLayout = new QHBoxLayout(); pageTurnLayout->addWidget(m_upPageBtn); pageTurnLayout->addSpacing(35); pageTurnLayout->addWidget(m_currentPage); pageTurnLayout->addSpacing(35); pageTurnLayout->addWidget(m_downPageBtn); QHBoxLayout *pageJumpLayout = new QHBoxLayout(); pageJumpLayout->addWidget(m_jumpBox); pageJumpLayout->addSpacing(10); pageJumpLayout->addWidget(m_jumpBtn); pageJumpLayout->setContentsMargins(5, 5, 5, 5); // 布局边距 QHBoxLayout *pageLayout = new QHBoxLayout(); pageLayout->addLayout(pageTurnLayout); pageLayout->addLayout(pageJumpLayout); // 主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addLayout(buttonLayout); mainLayout->addWidget(m_personTable); mainLayout->addLayout(pageLayout); mainLayout->setContentsMargins(10, 10, 10, 10); mainLayout->setSpacing(10); // 设置操作委托到操作列 EditDeleteButtonDelegate *delegate = new EditDeleteButtonDelegate(this); m_personTable->setItemDelegateForColumn(6, delegate); connect(delegate, &EditDeleteButtonDelegate::editClicked, this, [this](const QModelIndex &index) { onEditButtonClicked(index); }); connect(delegate, &EditDeleteButtonDelegate::deleteClicked, this, [this](const QModelIndex &index) { onDelButtonClicked(index); }); // 连接信号 connect(m_addPersonButton, &QPushButton::clicked, this, &PersonManagement::onAddPersonClicked); connect(m_exportButton, &QPushButton::clicked, this, &PersonManagement::onExportClicked); connect(m_importButton, &QPushButton::clicked, this, &PersonManagement::onImportClicked); connect(searchAction, &QAction::triggered, this, &PersonManagement::onSearchClicked); connect(m_searchLineEdit, &QLineEdit::returnPressed, this, &PersonManagement::onSearchClicked); }这个是我的界面代码,在这个代码基础上完成翻页功能和跳转功能
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值