Home Database Mysql Tutorial SQL查询时生成的临时文件对数据库查询的影响

SQL查询时生成的临时文件对数据库查询的影响

Jun 07, 2016 pm 04:21 PM
Influence database document Inquire generate

当使用sql语句进行查询时,查询的结果是存放在一个后缀名为tmp的一个临时文件中。当查询的时候,该文件存放查询的结果,当关闭该查询的时候,该临时文件会自动删除,所以在我们进行查询的时候,是感觉不到该文件的存在。 一、tmp文件存在于什么地方 对于wind

   当使用sql语句进行查询时,查询的结果是存放在一个后缀名为tmp的一个临时文件中。当查询的时候,该文件存放查询的结果,当关闭该查询的时候,该临时文件会自动删除,所以在我们进行查询的时候,是感觉不到该文件的存在。

  一、tmp文件存在于什么地方

  对于windows系统,都有一个系统环境变量,这个变量可以通过

  右击我的电脑——属性——高级——环境变量可以查看。

  Tmp临时文件就存在于temp文件夹下面。

  二、tmp文件的格式

  对于tmp文件,里面存放的查询结果,那么对于查询结果在tmp文件中的存储是按照

  一定格式来进行存储的,存储的格式为:

  对于每一列,分成两部分:

  第一部分:标示该列的长度,当字符的长度大于255时,使用5个字节来存放。

  第二部分:该列的数据。对于字符型数据,是转化为uniocode来进行存储的。

select 1,cast(1 as bigint),'ab',getdate() 
查询生成的tmp文件为(16进制) 
04 01 00 00 00 08 01 00 00 00 00 00 00 00 04 61 00 62 00 08 60 73 c2 02 7a 7b cb 08
其中04 01 00 00 00为第一个1 
08 01 00 00 00 00 00 00 00为第二个bigint类型的1 
04 61 00 62 00 为’ab’ 
08 60 73 c2 02 7a 7b cb 08 为getdate()

  例如:对于这样一张表:

SQL查询时生成的临时文件对数据库查询的影响

  使用select * from tb 形成的tmp文件格式为:

id列的数据长度

id列的数据

dtcol列的数据长度

dtcol列的数据

charcol的数据长度

charcol的数据

maxcol列的数据长度

maxcol的数据

  其中charcol与maxcol都是转换为unicode来进行存储的。

  对于tmp文件的查看,可以通过记事本来查看,但是只能看到字符,对于数字、日期看到的是乱码,可以使用UE来查看tmp文件的二进制数据。

  三、向该表中插入100W数据

declare @i int @dt datetime

select @i=0,@dt='1900-1-1'

while @i

begin

   insert into testdata(dtcol,charcol,maxcol)

   values(@dt+@i,replicate(char(rand()*26+65),100),replicate(newid(),100))

   set @i=@i+1

end

  四、tmp文件对查询的影响

  在知道了tmp文件的格式之后,,那么对于tmp文件的大小一般是能估算出来的,以上

  述表为例,一行在tmp文件中的大小为:1+8+1+8+1+200+5+7200=7424B,100W的数据大约是7424*100WB,tmp文件的大小为7,250,000KB左右。

  1、当表中的数据比较多的话,尤其是字符类型的数据占多数的时候,就需要注意这个tmp文件了。如果temp文件夹所在的磁盘空间不富裕的话,那么tmp会占用剩余的磁盘空间,还不够的话,那么系统会提示空间不够,并且会终止本次查询。

  2、temp文件夹所在的磁盘的磁盘格式最好为NTFS,因为FAT32格式最大的文件大小为4G,当tmp文件的大小超过4G时,那么是不会产生新的tmp文件的,那么系统也会提示空间不够,并且终止本次查询。(空间不够未必是磁盘空间不够,而是因为tmp文件已经达到最大容量4G)

  综上所述:temp文件夹应该放在磁盘空间充足的,并且分区格式为ntfs格式的分区上。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles