# 示例查询

示例查询（Query by example，QBE）是一种检索方法，用户提供一个样本对象，例如表格骨架、图像或填写了部分字段的记录，系统返回与示例匹配或相似的数据库条目，而不要求用户提供关键词字符串或形式化查询语法。该术语涵盖两个相关含义：莫什·兹卢夫基于表格的数据库查询语言，以及更广义的基于内容的示例检索——在示例与已存储条目之间计算相似度。<sup>[1](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)</sup><sup> • </sup><sup>[2](http://www.igi-global.com/dictionary/query-by-example-qbe/24385)</sup> 在多媒体检索中，QBE 查询包含一个或多个媒体对象作为示例，意图是找到相似对象<sup>[2](http://www.igi-global.com/dictionary/query-by-example-qbe/24385)</sup>；在图像数据库中，示例可以是一张数字照片、用户绘制的粗略示例、线条草图，或仅附带约束条件的空图像。<sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup> 这类基于内容的图像检索自 1992 年以来一直是活跃的研究领域。<sup>[4](https://ar5iv.labs.arxiv.org/html/2011.06490)</sup>

| 关键事实 | 详情 |
|---|---|
| 原始形式 | 兹卢夫的 QBE，一种用于查询、更新、定义和控制关系数据库的高级语言，首次在 AFIPS '75（1975年5月19–22日）上提出<sup>[5](https://dl.acm.org/doi/10.1145/1499949.1500034)</sup><sup> • </sup><sup>[1](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)</sup> |
| 查询输入 | 表格骨架、示例图像、草图，或填入了字段的探测记录<sup>[1](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)</sup><sup> • </sup><sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup> |
| 相似度度量 | 图像距离（零表示精确匹配），或语义多项分布之间的 Kullback–Leibler 散度<sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup><sup> • </sup><sup>[6](http://www.svcl.ucsd.edu/publications/conference/2006/civr06/QBSE.pdf)</sup> |
| 典型工作流 | 示例获取、查询学习、候选排序和调整反馈<sup>[7](https://www.sciencedirect.com/science/article/abs/pii/S0306437918300978)</sup> |
| 大规模索引 | 基于图的 HNSW 广泛用于生产环境的近似最近邻系统；精确搜索每次查询的代价为 \( O(n \cdot d) \)<sup>[8](https://arxiv.org/pdf/2608.12812.pdf)</sup> |
| 实际部署 | Microsoft Access（“可视化示例查询”）、SQL Server Enterprise Manager、Spring Data 和相似图像搜索<sup>[9](https://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/qbe.pdf)</sup><sup> • </sup><sup>[2](http://www.igi-global.com/dictionary/query-by-example-qbe/24385)</sup> |
| 主要失效模式 | 语义差距：从视觉数据中可提取的信息与用户解释之间的不匹配<sup>[4](https://ar5iv.labs.arxiv.org/html/2011.06490)</sup> |

## 工作原理

在兹卢夫的原始语言中，用户在屏幕上填写表格骨架。标记“P.”代表打印，指示期望的输出；带下划线的示例元素（如 ROD）是代表可能答案的变量，而常量（如 GREEN）则陈述所需条件。示例元素还可以跨两个或多个表链接行，连接操作即以此方式表达。<sup>[1](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)</sup> 当不需要行间链接时，查询无需示例元素，因为独立的 P. 就是默认情况。<sup>[1](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)</sup>

在基于内容的示例检索中，相似度由图像距离度量来判定：距离为零表示精确匹配，值越大表示相似程度越低，引擎按与查询的距离排序返回图像。<sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup> 使用四大类相似度度量：颜色、纹理、形状，以及对象与关系相似度。<sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup> 一种语义变体——按语义示例查询（QBSE）——将每张图像表示为语义多项分布，即后验概念概率的向量，并检索在概率单纯形上的投影最接近查询图像投影的数据库图像；由于这些是概率分布，自然的相似度函数是 [Kullback–Leibler 散度](https://www.edgepedia.org/zh/kullback-leibler-divergence)。<sup>[6](http://www.svcl.ucsd.edu/publications/conference/2006/civr06/QBSE.pdf)</sup> 检索于是转化为最小化查询的语义多项分布与每个数据集图像的语义多项分布之间的散度。<sup>[10](http://www.svcl.ucsd.edu/publications/conference/2008/mir08/mir08.pdf)</sup>

## 实现方法

典型的 QBE 应用分四个阶段运行：示例获取、查询学习、候选排序和调整反馈。<sup>[7](https://www.sciencedirect.com/science/article/abs/pii/S0306437918300978)</sup> 在图像检索中，过程从初始选取参考图像开始，参考图像随机选取或作为代表性样本选取，系统返回按颜色和纹理特征相似度评分的前 k 张最佳图像，在所研究的系统中两者等权重。<sup>[11](https://cci.drexel.edu/faculty/cyang/papers/yang2004a.pdf)</sup> 相关性反馈常用于泛化 QBE：用户将返回的图像分类为正匹配或负匹配，查询被重新表述，从而实现在线学习。<sup>[12](https://www.cs.auckland.ac.nz/courses/compsci708s1c/lectures/Glect-html/topic2c708FSC.htm)</sup>

在大规模场景下，精确最近邻搜索每次查询的代价为 \( O(n \cdot d) \)，当 n 达到数十亿、d 达到数百时，这一代价过高，因此近似方法以精确性换取效率。基于图的 HNSW 广泛用于生产系统，经验性能强劲，尽管缺乏最坏情况下的对数搜索保证；DiskANN 则扩展到十亿级 SSD 支持的索引。<sup>[8](https://arxiv.org/pdf/2608.12812.pdf)</sup><sup> • </sup><sup>[25](https://proceedings.neurips.cc/paper_files/paper/2023/file/d0ac28b79816b51124fcc804b2496a36-Paper-Conference.pdf)</sup> 基于树的方法（如 KD 树和球树）在超过约 20 维时性能退化，逐渐接近线性扫描；乘积量化将向量压缩为子向量码本，以一定的召回率损失换取内存高效的近似距离计算。<sup>[8](https://arxiv.org/pdf/2608.12812.pdf)</sup>

## 起源

QBE 由 IBM 的 M. M. 兹卢夫创建，他于 1975 年在 AFIPS '75 上首次提出，并于 1977 年在《IBM Systems Journal》上以论文《示例查询：一种数据库语言》发表。<sup>[13](https://doi.org/10.1147/sj.164.0324)</sup><sup> • </sup><sup>[5](https://dl.acm.org/doi/10.1145/1499949.1500034)</sup> IBM 将 QBE 描述为一种供非程序员查询关系数据库使用的查询语言；1975 年 VLDB 的后续研究表明，其操作不仅限于查询，还扩展到定义数据库（包括数据描述和完整性约束），以及初步探查——检索超大型数据库的子集以便于表述具体查询。<sup>[14](https://research.ibm.com/publications/query-by-example-the-invocation-and-definition-of-tables-and-forms)</sup> 1976 年的一项扩展表明 QBE 操作与数据库结构无关：在层次视图中，用户在层次结构的骨架中填写可能答案的示例；对于没有显示设备的情况，还提供了线性版本。<sup>[15](https://research.ibm.com/publications/query-by-example-operations-on-hierarchical-data-bases)</sup>

AFIPS '75 的一项配套研究通过实验证明了非程序员学习和使用该语言的简便性与准确性，并将 QBE 归类为一种不试图模仿英语的形式语言，这与 IBM 的交互式查询设施（IQF）和 SEQUEL 不同。<sup>[16](https://dl.acm.org/doi/10.1145/1499949.1500035)</sup> QBE 系统得以开发<sup>[3](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)</sup>，该术语后来被转用于图像和其他媒体的基于内容检索。

## 变体

[基于内容的图像检索](https://www.edgepedia.org/zh/content-based-image-retrieval)区分出一种基于视觉查询的范式，称为按视觉示例查询（QBVE）。<sup>[17](https://dl.acm.org/doi/10.1109/2.410145)</sup> QBSE 将 QBVE 与语义检索相结合，将每张图像表示为后验概念概率的语义多项分布，已被证明在语义空间内外均显著优于 QBVE。<sup>[18](https://escholarship.org/content/qt4bj3k85j/qt4bj3k85j.pdf)</sup> 按上下文示例查询（QBCE）通过利用概念出现的上下文扩展了这一路线。<sup>[10](http://www.svcl.ucsd.edu/publications/conference/2008/mir08/mir08.pdf)</sup>

按草图查询（QBS）与 QBE 非常相似：两者通常都依赖颜色和纹理等低层特征而非语义。<sup>[12](https://www.cs.auckland.ac.nz/courses/compsci708s1c/lectures/Glect-html/topic2c708FSC.htm)</sup> 由于单个示例很少包含所有需要的特征，自然的扩展是接受多个示例，将它们汇集为联合伪示例。<sup>[12](https://www.cs.auckland.ac.nz/courses/compsci708s1c/lectures/Glect-html/topic2c708FSC.htm)</sup> 在数据库领域，现代 QBE 的发展让用户提供一组数据示例，从中逆向工程（即学习）出查询。<sup>[7](https://www.sciencedirect.com/science/article/abs/pii/S0306437918300978)</sup>

## 应用

QBE 在多个关系数据库前端中得到支持，尤其是 [Microsoft Access](https://www.edgepedia.org/zh/microsoft-access)（实现了“可视化示例查询”）以及 Microsoft SQL Server Enterprise Manager。<sup>[9](https://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/qbe.pdf)</sup> Spring Data 为应用代码提供了示例查询 API。<sup>[19](https://docs.spring.io/spring-data/commons/reference/4.2-SNAPSHOT/query-by-example.html)</sup> 在多媒体搜索中，QBE 是 Google“查找相似页面”和基于图像的相似图像搜索等功能的基础。<sup>[2](http://www.igi-global.com/dictionary/query-by-example-qbe/24385)</sup>

Spring Data 的 API 由四部分组成：Probe，即填入了字段的实际示例领域对象；ExampleMatcher，携带如何匹配特定字段的细节；Example，组合探测对象和匹配器；以及 FetchableFluentQuery，用于链式定制。<sup>[19](https://docs.spring.io/spring-data/commons/reference/4.2-SNAPSHOT/query-by-example.html)</sup>

## 局限与替代方案

基于示例的图像检索的核心失效模式是语义差距，定义为从视觉数据中可提取的信息与用户对其所作解释之间的不一致。<sup>[4](https://ar5iv.labs.arxiv.org/html/2011.06490)</sup> QBE 还严重依赖初始图像集：在大型数据库中，找到一组至少包含一张相关图像的初始图像可能很困难，这种情况称为“第零页问题”。<sup>[11](https://cci.drexel.edu/faculty/cyang/papers/yang2004a.pdf)</sup> QBE 和按草图查询通常都基于低层特征而非语义<sup>[12](https://www.cs.auckland.ac.nz/courses/compsci708s1c/lectures/Glect-html/topic2c708FSC.htm)</sup>，Spring Data 的 QBE 不支持嵌套或分组的属性约束，例如 firstname = ?0 或 (firstname = ?1 and lastname = ?2)。<sup>[19](https://docs.spring.io/spring-data/commons/reference/4.2-SNAPSHOT/query-by-example.html)</sup> QBE 适合不太复杂、可用少量表表达的查询。<sup>[9](https://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/qbe.pdf)</sup>

与关键词搜索和 SQL 相比，示例检索接受对象作为查询，这对多媒体内容往往更直接。<sup>[4](https://ar5iv.labs.arxiv.org/html/2011.06490)</sup> 较新的系统扩展了示例范式本身：FaBle 使用 LLM 生成 facet 级（不）相关文档对，在 1K 文档上的增强有助于训练面向分面 QBE 检索的 facet 条件嵌入<sup>[20](https://arxiv.org/html/2412.01443v1)</sup>；MetaEmbed 用可学习的元词元 以多向量后期交互取代单向量检索，让用户在测试时权衡准确性与延迟<sup>[21](https://arxiv.org/html/2509.18095)</sup>，其基础是 库苏帕蒂及其同事（2022）的 Matryoshka 表示学习<sup>[22](https://doi.org/10.48550/arxiv.2205.13147)</sup>；OneDB 为每种模态赋予各自的度量空间和距离函数，并扩展 Spark SQL 增加 ODBRANGE 算子以支持多度量范围查询<sup>[23](https://arxiv.org/html/2507.04256v1)</sup>；多模态 LLM 充当免训练的重排序器，将“是”和“否”的下一词元概率转换为相似度分数。<sup>[24](https://openaccess.thecvf.com/content/CVPR2026F/papers/Tharwat_Indexing_Multimodal_Language_Models_for_Large-scale_Image_Retrieval_CVPRF_2026_paper.pdf)</sup>

## References

1. [Query-by-Example: A data base language (M. M. Zloof, IBM Systems Journal 16(4))](http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/IBM_Systems_Journal/164/ibmsj1604C.pdf)
2. [What is Query-by-Example (QBE) | IGI Global](http://www.igi-global.com/dictionary/query-by-example-qbe/24385)
3. [Content-Based Image Retrieval (course textbook chapter, University of Washington CSE 576)](https://courses.cs.washington.edu/courses/cse576/book/ch8.pdf)
4. [Content-based Image Retrieval and the Semantic Gap in the Deep Learning Era](https://ar5iv.labs.arxiv.org/html/2011.06490)
5. [Query by example (Moshé M. Zloof, AFIPS '75, pp. 431-438)](https://dl.acm.org/doi/10.1145/1499949.1500034)
6. [Query by Semantic Example (LNCS 4071, CIVR 2006)](http://www.svcl.ucsd.edu/publications/conference/2006/civr06/QBSE.pdf)
7. [Reverse engineering database queries from examples: State-of-the-art, challenges, and research opportunities (Information Systems)](https://www.sciencedirect.com/science/article/abs/pii/S0306437918300978)
8. [A Comprehensive Empirical Evaluation of Vector Database Systems for Approximate Nearest Neighbor Search](https://arxiv.org/pdf/2608.12812.pdf)
9. [QBE (textbook supplement, Database System Concepts)](https://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/qbe.pdf)
10. [Image Retrieval using Query by Contextual Example (MIR 2008, UCSD SVCL)](http://www.svcl.ucsd.edu/publications/conference/2008/mir08/mir08.pdf)
11. [Content-based image retrieval: a comparison between query by example and image browsing map approaches (Journal of Information Science, 2004)](https://cci.drexel.edu/faculty/cyang/papers/yang2004a.pdf)
12. [COMPSCI 708: QBE and Content Representation (University of Auckland lecture notes)](https://www.cs.auckland.ac.nz/courses/compsci708s1c/lectures/Glect-html/topic2c708FSC.htm)
13. [M. M. Zloof (1977). Query-by-Example: A data base language. IBM Systems Journal.](https://doi.org/10.1147/sj.164.0324)
14. [Query-by-example: The invocation and definition of tables and forms (VLDB 1975, IBM Research)](https://research.ibm.com/publications/query-by-example-the-invocation-and-definition-of-tables-and-forms)
15. [Query-by-example, operations on hierarchical data bases (AFIPS/NCC 1976, IBM Research)](https://research.ibm.com/publications/query-by-example-operations-on-hierarchical-data-bases)
16. [A psychological study of query by example (AFIPS '75)](https://dl.acm.org/doi/10.1145/1499949.1500035)
17. [Content-Based Image Retrieval Systems (Computer magazine, IEEE)](https://dl.acm.org/doi/10.1109/2.410145)
18. [Bridging the gap: Query by semantic example](https://escholarship.org/content/qt4bj3k85j/qt4bj3k85j.pdf)
19. [Query by Example :: Spring Data Commons (official documentation)](https://docs.spring.io/spring-data/commons/reference/4.2-SNAPSHOT/query-by-example.html)
20. [Multi-Facet Blending for Faceted Query-by-Example Retrieval (FaBle)](https://arxiv.org/html/2412.01443v1)
21. [MetaEmbed: Scaling Multimodal Retrieval at Test-Time with Flexible Late Interaction](https://arxiv.org/html/2509.18095)
22. [Kusupati, Aditya and colleagues (2022). Matryoshka Representation Learning. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2205.13147)
23. [OneDB: A Distributed Multi-Metric Data Similarity Search System](https://arxiv.org/html/2507.04256v1)
24. [Indexing Multimodal Language Models for Large-scale Image Retrieval (CVPR 2026)](https://openaccess.thecvf.com/content/CVPR2026F/papers/Tharwat_Indexing_Multimodal_Language_Models_for_Large-scale_Image_Retrieval_CVPRF_2026_paper.pdf)
25. [D0ac28b79816b51124fcc804b2496a36 Paper Conference (proceedings.neurips.cc)](https://proceedings.neurips.cc/paper_files/paper/2023/file/d0ac28b79816b51124fcc804b2496a36-Paper-Conference.pdf)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026; Oct 21, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › SQL and query languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/query-by-example

English original: https://www.edgepedia.org/query-by-example
