6.1. Compaction

The compaction operation is the way to reduce disk space usage by removingunused and old data from database or view index files. This operation is verysimilar to the vacuum (SQLite ex.) operation available for other databasemanagement systems.

During compaction of the target CouchDB creates new file with the .compactextension and transfers only actual data into. Because of this, CouchDB checksfirst for the available disk space - it should be twice greater than thecompacted file’s data.

When all actual data is successfully transferred to the compacted file CouchDBreplaces the target with the compacted file.

6.1.1. Database Compaction

Database compaction compresses the database file by removing unused filesections created during updates. Old documents revisions are replaced withsmall amount of metadata called tombstone which are used for conflictsresolution during replication. The number of stored revisions(and their tombstones) can be configured by using the _revs_limit URL endpoint.

Compaction is manually triggered operation per database and runs as a backgroundtask. To start it for specific database there is need to send HTTPPOST /{db}/_compact sub-resource of the target database:

  1. curl -H "Content-Type: application/json" -X POST http://localhost:5984/my_db/_compact

On success, HTTP status 202 Accepted is returned immediately:

  1. HTTP/1.1 202 Accepted
  2. Cache-Control: must-revalidate
  3. Content-Length: 12
  4. Content-Type: text/plain; charset=utf-8
  5. Date: Wed, 19 Jun 2013 09:43:52 GMT
  6. Server: CouchDB (Erlang/OTP)
  1. {"ok":true}

Although the request body is not used you must still specifyContent-Type header with application/json valuefor the request. If you don’t, you will be aware about with HTTP status415 Unsupported Media Type response:

  1. HTTP/1.1 415 Unsupported Media Type
  2. Cache-Control: must-revalidate
  3. Content-Length: 78
  4. Content-Type: application/json
  5. Date: Wed, 19 Jun 2013 09:43:44 GMT
  6. Server: CouchDB (Erlang/OTP)
  7.  
  8. {"error":"bad_content_type","reason":"Content-Type must be application/json"}

When the compaction is successful started and running it is possible to getinformation about it via database information resource:

  1. curl http://localhost:5984/my_db
  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 246
  4. Content-Type: application/json
  5. Date: Wed, 19 Jun 2013 16:51:20 GMT
  6. Server: CouchDB (Erlang/OTP)
  7.  
  8. {
  9. "committed_update_seq": 76215,
  10. "compact_running": true,
  11. "data_size": 3787996,
  12. "db_name": "my_db",
  13. "disk_format_version": 6,
  14. "disk_size": 17703025,
  15. "doc_count": 5091,
  16. "doc_del_count": 0,
  17. "instance_start_time": "0",
  18. "purge_seq": 0,
  19. "update_seq": 76215
  20. }

Note that compaction_running field is true indicating that compactionis actually running. To track the compaction progress you may query the_active_tasks resource:

  1. curl http://localhost:5984/_active_tasks
  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 175
  4. Content-Type: application/json
  5. Date: Wed, 19 Jun 2013 16:27:23 GMT
  6. Server: CouchDB (Erlang/OTP)
  7.  
  8. [
  9. {
  10. "changes_done": 44461,
  11. "database": "my_db",
  12. "pid": "<0.218.0>",
  13. "progress": 58,
  14. "started_on": 1371659228,
  15. "total_changes": 76215,
  16. "type": "database_compaction",
  17. "updated_on": 1371659241
  18. }
  19. ]

6.1.2. Views Compaction

Views are also need compaction like databases, unlike databases viewsare compacted by groups per design document. To start their compaction thereis need to send HTTP POST /{db}/_compact/{ddoc} request:

  1. curl -H "Content-Type: application/json" -X POST http://localhost:5984/dbname/_compact/designname
  1. {"ok":true}

This compacts the view index from the current version of the specified designdocument. The HTTP response code is 202 Accepted(like compaction for databases) and a compaction backgroundtask will be created.

6.1.2.1. Views cleanup

View indexes on disk are named after their MD5 hash of the view definition.When you change a view, old indexes remain on disk. To clean up all outdatedview indexes (files named after the MD5 representation of views, that does notexist anymore) you can trigger a view cleanup:

  1. curl -H "Content-Type: application/json" -X POST http://localhost:5984/dbname/_view_cleanup
  1. {"ok":true}

6.1.3. Automatic Compaction

While both database and viewscompactions are required be manually triggered, it is also possible to configureautomatic compaction, so that compaction of databases and views is automaticallytriggered based on various criteria. Automatic compaction is configured inCouchDB’s configuration files.

The daemons/compaction_daemon is responsible for triggeringthe compaction. It is enabled by default and automatically started.The criteria for triggering the compactions is configured in thecompactions section.

原文: http://docs.couchdb.org/en/stable/maintenance/compaction.html