> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pawsql.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> PawSQL 是一个产品：Cloud 是公网部署形态，Engine / Optimizer / Auditor / Advisor / Patroller 是同一产品的组件与交付形态，不是彼此独立的产品。 / PawSQL is a single product: Cloud is the public deployment form, while Engine / Optimizer / Auditor / Advisor / Patroller are components and delivery forms of the same product, not separate products.
> 术语以站内术语表为准：SQL 审核对应英文 SQL Review，查询重写对应 Query Rewrite，索引推荐对应 Index Recommendation；英文内容统一用 Review，不用 Audit。 / Use the site glossary for terminology: 审核 is SQL Review, 重写 is Query Rewrite, 索引推荐 is Index Recommendation; English content uses Review, never Audit.
> 引用能力范围或版本支持时以对应页面为准；标注 unknown、或 status 非 published 的内容表示尚未经产品核实，不应作为事实引用。 / Cite capability scope and version support from the corresponding page; content marked unknown, or with a status other than published, is not yet product-verified and must not be cited as fact.

# Create optimization task (asynchronous)

> Creates a SQL optimization task and returns immediately, polling for results via `/getAnalysisSummary`.
Suitable for long queries or scenarios where HTTP timeouts should be avoided.




## OpenAPI

````yaml /openapi/pawsql-optimization-en.yaml post /createAnalysisAsync
openapi: 3.0.3
info:
  title: PawSQL Optimization
  description: >
    PawSQL SQL optimization service external interfaces.


    The PawSQL optimization engine is a query optimization engine built for
    developers and DBAs, integrating database industry best practices for query
    optimization,

    providing powerful SQL auditing and rewrite optimization capabilities, and
    integrating the Paw Index Advisor index recommendation engine for cost-based
    index recommendations for slow queries.


    ## Authentication


    Most endpoints require authentication via `userKey` (activation code).

    The `userKey` can be obtained through the `/getUserKey` endpoint
    (email/password login).


    ## API Call Flow


    1. Call `/getUserKey` to obtain a userKey

    2. Call `/createWorkspace` to create a workspace (or pass DDL directly in
    `/createAnalysis`)

    3. Call `/createAnalysis` or `/createAnalysisAsync` to create an
    optimization task

    4. Call `/getAnalysisSummary` to obtain the optimization summary

    5. Call `/getStatementDetails` to obtain details for a single SQL statement
  version: 1.0.0
  contact:
    name: PawSQL Team
    url: https://pawsql.com
  license:
    name: PawSQL
    url: https://pawsql.com
servers:
  - url: /api/v1
    description: PawSQL Server
security:
  - userKey: []
tags:
  - name: Authentication
    description: Authentication endpoints
  - name: Workspace
    description: Workspace management endpoints
  - name: Optimization
    description: SQL optimization task endpoints
paths:
  /createAnalysisAsync:
    post:
      tags:
        - Optimization
      summary: Create optimization task (asynchronous)
      description: >
        Creates a SQL optimization task and returns immediately, polling for
        results via `/getAnalysisSummary`.

        Suitable for long queries or scenarios where HTTP timeouts should be
        avoided.
      operationId: createAnalysisAsync
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiAnalysisCreate'
            examples:
              example:
                value:
                  userKey: CB698418-88B25371-67F15F01-XXXXXXXX
                  workspace: '1730508173442965506'
                  workload: select * from organization where adm_id = 100;
                  queryMode: plain_sql
      responses:
        '200':
          description: Asynchronous optimization task creation result
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiResult'
                  - type: object
                    properties:
                      data:
                        $ref: '#/components/schemas/AnalysisCreateResponse'
components:
  schemas:
    ApiAnalysisCreate:
      type: object
      description: Create optimization task request
      required:
        - userKey
        - workspace
        - workload
        - queryMode
      properties:
        userKey:
          type: string
          description: Activation code
          example: CB698418-88B25371-67F15F01-XXXXXXXX
        workspace:
          type: string
          description: >-
            Workspace ID or DDL text (an offline workspace is auto-created when
            DDL is passed)
        dbType:
          type: string
          description: >
            Database type (displayCode); required when workspace is DDL, default
            mysql.

            See the dbType table in the /createWorkspace endpoint for supported
            values.
        workload:
          type: string
          description: SQL text to optimize
        queryMode:
          type: string
          description: 'SQL type: plain_sql / mysql_query_log / postgresql_query_log'
          enum:
            - plain_sql
            - mysql_query_log
            - postgresql_query_log
          example: plain_sql
        validateFlag:
          type: boolean
          description: >-
            Whether to run validate checks (only effective for online
            workspaces), default false
          default: false
        singleQueryFlag:
          type: boolean
          description: Whether to validate as a single SQL statement only
        analysisName:
          type: string
          description: Optimization task name; auto-generated when omitted
        analyzeFlag:
          type: boolean
          description: Whether to run analysis
        closeRewrite:
          type: boolean
          description: Whether to disable query rewriting
        deduplicateFlag:
          type: boolean
          description: Whether to deduplicate indexes
        updateStatsBeforeValidationFlag:
          type: boolean
          description: Whether to update statistics before validation
        indexOnly:
          type: boolean
          description: Whether to recommend indexes only (no rewriting)
        maxMembers:
          type: integer
          description: Maximum number of columns in a composite index
        maxMembersForIndexOnly:
          type: integer
          description: Maximum number of columns in a composite index in index-only mode
        maxPerTable:
          type: integer
          description: Maximum number of recommended indexes per table
        maxSpace:
          type: integer
          description: Maximum index space multiplier
        rules:
          type: array
          description: Custom rule list; the default audit rules are used when omitted
          items:
            $ref: '#/components/schemas/RuleBasicRead'
    ApiResult:
      type: object
      description: Unified response body
      properties:
        code:
          type: integer
          description: Status code, 200 indicates success
          example: 200
        message:
          type: string
          description: Description message
          example: Success
        data:
          description: Response data
    AnalysisCreateResponse:
      type: object
      description: Create optimization task response
      properties:
        analysisId:
          type: string
          description: Optimization task ID
          example: '1730505660018876418'
        status:
          type: string
          description: 'Optimization status: success / success0 / success1 / failed'
          example: success1
    RuleBasicRead:
      type: object
      description: Rule definition
      properties:
        ruleCode:
          type: string
          description: Rule code
        rewrite:
          type: boolean
          description: Whether to rewrite the rule
        threshold:
          type: string
          description: Rule threshold
  securitySchemes:
    userKey:
      type: apiKey
      in: header
      name: userKey
      description: Activation code, obtained via the /getUserKey endpoint

````