> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> ClickHouse 允许在发送 `SELECT` 查询的同时，向服务器发送查询处理所需的数据。这些数据会被放入临时表中，并可在查询中使用（例如在 `IN` 运算符中）。

# 用于查询处理的外部数据

ClickHouse 允许在发送 `SELECT` 查询的同时，向服务器发送查询处理所需的数据。这些数据会被放入临时表中 (参见“Temporary tables”一节) ，并可在查询中使用 (例如在 `IN` 运算符中) 。

例如，如果你有一个包含重要用户标识符的文本文件，可以将其与使用该列表进行过滤的查询一起上传到服务器。

如果你需要使用大量外部数据运行多个查询，请不要使用此功能。更好的做法是提前将数据上传到数据库中。

外部数据可以通过命令行客户端 (在非交互模式下) 上传，也可以通过 HTTP 接口上传。

在命令行客户端中，你可以按如下格式指定参数部分：

```bash theme={null}
--external --file=... [--name=...] [--format=...] [--types=...|--structure=...]
```

你可能会有多个这样的部分，具体数量取决于正在传输的表的数量。

**–external** – 标志着一个子句的开始。
**–file** – 包含表转储的文件路径，或 `-` (表示 stdin) 。
stdin 中只能读取单个表。

以下参数为可选参数：**–name**– 表名。如果省略，则使用 \_data。
**–format** – 文件中的数据格式。如果省略，则使用 TabSeparated。

以下参数中必须指定一个：**–types** – 以逗号分隔的列类型列表。例如：`UInt64,String`。这些列将命名为 \_1、\_2、...
**–structure**– 表结构，格式如 `UserID UInt64`、`URL String`。用于定义列名和类型。

在 'file' 中指定的文件将按 'format' 指定的格式进行解析，并使用 'types' 或 'structure' 中指定的数据类型。该表会被上传到服务器，并可在服务器上以名为 'name' 的临时表进行访问。

示例：

```bash theme={null}
$ echo -ne "1\n2\n3\n" | clickhouse-client --query="SELECT count() FROM test.visits WHERE TraficSourceID IN _data" --external --file=- --types=Int8
849897
$ cat /etc/passwd | sed 's/:/\t/g' | clickhouse-client --query="SELECT shell, count() AS c FROM passwd GROUP BY shell ORDER BY c DESC" --external --file=- --name=passwd --structure='login String, unused String, uid UInt16, gid UInt16, comment String, home String, shell String'
/bin/sh 20
/bin/false      5
/bin/bash       4
/usr/sbin/nologin       1
/bin/sync       1
```

使用 HTTP 接口时，外部数据通过 multipart/form-data 格式传入。每个表都会作为单独的文件传输，表名取自文件名。`query_string` 中会传递参数 `name_format`、`name_types` 和 `name_structure`，其中 `name` 是这些参数对应的表名。这些参数的含义与使用命令行客户端时相同。

示例：

```bash theme={null}
$ cat /etc/passwd | sed 's/:/\t/g' > passwd.tsv

$ curl -F 'passwd=@passwd.tsv;' 'http://localhost:8123/?query=SELECT+shell,+count()+AS+c+FROM+passwd+GROUP+BY+shell+ORDER+BY+c+DESC&passwd_structure=login+String,+unused+String,+uid+UInt16,+gid+UInt16,+comment+String,+home+String,+shell+String'
/bin/sh 20
/bin/false      5
/bin/bash       4
/usr/sbin/nologin       1
/bin/sync       1
```

在分布式查询处理中，临时表会被发送到所有远程服务器。
