使用工作表查询数据

After you create or open a worksheet, you can manage the worksheet, write and execute queries, explore query results and history, and set up filters using Snowsight.

在工作表中编写查询

打开工作表后,您可以编写 SQL 查询和语句。

Note

不支持在单个 API 调用中使用多个 SQL 语句。确保工作表中的每个 SQL 查询都以单个分号 (;) 结尾。

设置工作表上下文

将数据库和数据库架构(可选)设置为工作表上下文时,您可以引用架构中的对象,无需完全限定查询中的对象名称。

使用 Autocomplete 编写查询

在查询编辑器中输入脚本时,Autocomplete 功能会建议:

  • 查询语法关键字,例如 SQL 函数或别名。
  • 与架构中的表或列名称匹配的值。

选择一个函数以查看其语法和简要描述。

Snowflake tracks table aliases and suggests them as autocomplete options. For example, if you execute a query using posts as p or posts p as an alias, the next time you type p, the autocomplete feature suggests the alias as an option.

Use Snowflake Copilot to write queries

Snowflake Copilot is an LLM-powered assistant that simplifies data analysis. You can use natural language requests to explore a new dataset, generate queries or refine existing queries.

See Using Snowflake Copilot to learn more about Snowflake Copilot and for example prompts to get you started.

将 SQL 脚本追加到现有工作表

如果文件中有 SQL 脚本,您可以执行以下操作,将脚本追加到现有工作表:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Worksheets.
  3. 打开工作表。
  4. Hover over the tab for the worksheet and select More actions for worksheet, then choose Import SQL from File.
  5. 浏览计算机上的文件 SQL。

文件内容将追加到工作表。

参阅工作表中的数据库对象名称

While you write queries in your worksheet, refer to the database objects relevant to the queries in the Databases explorer. You can drill down to specific database objects, or use search to locate a database, schema, or object that you have access to.

Using the Databases explorer, you can pin databases and database objects for quick reference. When you hover over a database object, select the Pin icon to pin them. Pinned objects appear at the top of the Databases explorer in the Pinned section. You might need to expand the section to view of all your pinned objects.

找到某个数据库对象后,您可以将该对象的名称放在正编辑的工作表中:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Worksheets.
  3. 打开工作表。
  4. Locate the database object in the Databases explorer.
  5. Hover over the object name and select more menu » Place Name in Editor.

完全限定的对象名称在工作表中的光标位置之后显示。

对于数据库表和视图,您还可以将列名称添加到正在编辑的工作表中:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Worksheets.
  3. 打开工作表。
  4. Locate the database object in the Databases explorer.
  5. Hover over the object name and select more menu » Add Columns in Editor.

逗号分隔的列名称在工作表中的光标位置之后显示。

设置查询的格式

工作表打开时,您可以选择工作表的名称,以设置工作表中查询的格式,并查看键盘快捷键。

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Worksheets.
  3. 打开工作表。
  4. Hover over the tab for the worksheet and select More actions for worksheet.
  5. In the drop-down list, select Format query to format the query text for readability.

将数据加载到表中

如果您使用的是工作表,并且想要添加一些数据来使用,您可以在不退出工作表的情况下将数据加载到表中:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Worksheets.
  3. 打开工作表。
  4. Select Objects to view the object explorer.
  5. 使用搜索或浏览来查找特定表。
  6. Hover over a specific table name and select More options » Load Data.
  7. 按照提示上传一个或多个 50MB 或更小的结构化或非结构化文件。

Refer to Load data using Snowsight for more details.

执行和运行查询

您可以在同一工作表中按顺序运行单个查询或多个查询。

  • To run a single query, in the query editor link, place your cursor in the query, and then select the Run button.
  • To run the entire worksheet, from the More options dropdown menu next to the Run button, select Run All.

在文件夹中运行工作表

Folders no longer have a role assigned to them. An owner or editor of a worksheet in a folder can change the worksheet to run as any role. You can also add USE ROLE to a worksheet in a folder to run different statements in the worksheet as different roles.

在文件夹内创建工作表时,使用当前会话的角色创建工作表。

Note

To run a worksheet in a folder that was shared with you, even if you have View and Run or Edit permissions on the folder, you must use the same role as the worksheet. If you do not have the same role, duplicate the worksheet and run it as one of your own roles.

浏览工作表结果

Note

适用于大多数账户。美国政府区域内的账户、使用 Virtual Private Snowflake (VPS) 的账户,以及使用专用连接访问 Snowflake 的账户仍能看到限制为 10,000 行的查询结果。

在工作表中运行一个查询或所有查询时,您会看到查询结果。

查询结果以表的形式显示。您可以使用键盘上的箭头键导航查询结果,就像使用电子表格一样。您可以在结果表中选择列、单元格、行或区域。您可以复制和粘贴任何选择。

For up to 1 million rows of results, you can review generated statistics that display contextual information for any selection, as well as overall statistics. See 自动上下文统计信息 for more details.

If you want to view your results as a chart, select Chart. For more details about charts, see Visualizing worksheet data.

Query results are cached. For more details, see Stored results for past worksheet versions and Manage worksheet history and versions.

转换查询结果的成本注意事项

Note

适用于大多数账户。美国政府区域内的账户、使用 Virtual Private Snowflake (VPS) 的账户,以及使用专用连接访问 Snowflake 的账户在转换查询结果时不会收费。

Note that some column transformation activities performed on the query results of Snowsight worksheets incur compute cost. The compute cost is billed against the same warehouse used to run the query.

例如,当您使用列选项按升序或降序对列进行排序时,更改会影响所有结果,而不仅仅是返回的前 10,000 行,并且会产生计算成本。

When you select a column, arrows display that you can use to sort a column ascending or descending.

To identify the interactions that incur compute cost, filter the Query History page to view only SQL statements that contain the SQL Text: snowsight_transform_cte.

The following transformations do not incur cost:

  • 显示数值列的千位分隔符。
  • 以百分比形式显示列。
  • 增加或减少小数点精度。
  • 设置日期和时间戳列的格式。

In addition, transformations performed by the recipient of a shared worksheet operating on a limited set of results do not incur cost. For more details about shared worksheet results, see Viewing results for past runs of a worksheet.

For more details about compute cost, see Exploring compute cost.

自动上下文统计信息

选择结果表中的列、单元格、行或区域,在检查器窗格(结果表的右侧)中查看有关所选数据的相关信息。系统会自动为所有列类型生成上下文统计信息。统计信息旨在帮助您一目了然地理解数据。

列概览显示每列的统计信息预览。从检查器或列标题中选择一列,以查看详细的列统计信息。

统计信息窗格为不同类型的列生成不同的指标。您可以使用统计信息窗格中的项目进行交互和筛选。

Filled/empty meters

所有列都显示已填充和为空的行数。显示某些数据类型(例如电子邮件和 JSON)的列也指明无效行数。

Histograms

针对所有日期、时间和数值列显示。

直方图指示属于特定范围的行。点击某个条形或在直方图上拖动可选择范围。您可以点击直方图上方的值标签以输入特定值,从而微调选择。

Frequency distributions

针对所有分类列显示。分类列是多次使用相同值的文本列。

Email domain distributions

针对电子邮件列显示。电子邮件域分布显示域名的出现频率分布。

Key distributions

针对 JSON 列显示。如果所有行都包含 JSON 对象,则键分布显示结果集中存在的顶级键的频率。如果列包含 JSON 数组,则键分布将显示列中 JSON 值的相对类型。

查看查询详细信息

The Query Details includes information about the execution of the query, including:

  • 查询执行的持续时间。
  • 结果中的行数。
  • 执行完成时间。
  • 查询扫描的数据量。
  • 用于执行查询的角色。
  • 用于执行查询的仓库。

Some query details are available for only 14 days.

查看查询配置文件

To access a detailed profile of your query, on the Query Details pane select the more menu » View Query Profile.

查询配置文件会在新的浏览器选项卡中打开。

For information on reviewing the query profile, see Review Query Profile.

下载查询结果

To download your query results as a CSV-formatted or TSV-formatted file, select Download results.

文件的大小取决于查询返回的数据量。Snowflake 不限制为查询结果导出的文件的大小。

查看查询历史记录

在工作表中运行 SQL 后,您可以查看在工作表中运行的查询的历史记录,例如比较不同查询运行的结果。您必须使用与工作表相同的角色,才能查看工作表的查询历史记录。

When the Results pane is visible, select Query history (Query history) to review the queries that have been run in the worksheet, as well as the results for those queries. The history includes up to 25 queries run in that worksheet during your current session and previous sessions over the last 14 days.

您可以查看以下信息:

  • 正在进行的查询的状态。
  • 查询的运行时间。
  • 查询的运行时长,以毫秒或秒为单位。
  • 运行的查询。
  • 查询 ID。

Select a row to see the results for that query execution in the Results pane. If you do not have the primary role used to run a query that you view in Query history, you cannot view the results for that query. Subqueries spawned by stored procedures or Python worksheets do not display.

要按状态、仓库或其他方面筛选工作表的查询历史记录,请执行以下操作:

  • Filter the query executions by status. For example, review queries that are still in the Running or Queued status and do not yet display results.
  • Select Show or hide filter to filter by warehouse, SQL text in the query, a specific query ID, or a duration greater than a specific time period.

Hover over a query execution row to see a full preview of the SQL statement that was run, copy the query ID, and optionally open the query details for the query execution. See Review Query History in Snowsight for more information about query details.

Query history data redacted from a Snowflake Native App

For queries related to a Snowflake Native App, the query_text and error_message fields are redacted from the query history in the following contexts:

  • Queries run when the app is installed or upgraded.
  • Queries that originate from a child job of a stored procedure owned by the app.

In each of these situations, the cell of the query history in Snowsight appears blank.