<span id="查看-sql-历史与画像"></span>

# Inspect SQL History and Profiles

Use the AI Studio SDK to paginate through workspace SQL execution history, obtain a statement ID, and read execution details and profiles for that statement.

(sdk-ai-studio-sql-history-flow)=
<span id="任务流程"></span>

## Task flow

First, read execution history pages through the workspace SQL service to obtain a target statement ID; then query execution status, error details, and the execution profile for that statement ID.

```text
Workspace SQL Service
         │
         ▼
Query SQL execution history (offset + limit)
         │
         ▼
    Statement ID
  ├── Read execution details (statement status and error text)
  └── Read execution profile (profile availability and message)
```

(sdk-ai-studio-sql-history-prepare)=
<span id="准备"></span>

## Prerequisites

1. Complete the endpoint, personal access token, and workspace setup in [Use the SDK](../../../quickstart/ai-studio/sdk.md).
2. **Statement ID** (when reading details and a profile): obtain one from the SQL execution history step below, or record the statement ID returned in [Run SQL Queries](../数据处理/执行SQL查询.md).

(sdk-ai-studio-sql-history-s1)=
<span id="查询-sql-执行历史"></span>

## Query SQL execution history

The example below reads up to 20 recent SQL execution records starting from offset `0`, printing the total count along with each record's statement ID, database name, status, and error message. To read subsequent pages, advance the offset by the number of returned records and stop when an empty list is returned.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
import os

import moi_product_sdk as sdk

client = sdk.new_with_personal_access_token(
    os.environ["PRODUCT_API_BASE_URL"],
    os.environ["PRODUCT_API_KEY"],
)
workspace = client.workspace(os.environ["WORKSPACE_ID"])
service = workspace.sql()

page = service.query_history(
    sdk.with_query_history_offset(0),
    sdk.with_query_history_limit(20),
)
print("Total:", page.total, "Offset:", page.offset, "Limit:", page.limit)
for record in page.query_list:
    print(record.statement_id, record.database, record.status, record.error)
```

::::

::::{tab-item} Go
:sync: go

```go
package main

import (
	"context"
	"fmt"
	"os"

	sdk "github.com/matrixorigin/matrixflow/sdk/go-sdk"
)

func main() {
	ctx := context.Background()
	client, err := sdk.NewWithPersonalAccessToken(
		os.Getenv("PRODUCT_API_BASE_URL"),
		os.Getenv("PRODUCT_API_KEY"),
	)
	if err != nil {
		panic(err)
	}
	workspace, err := client.Workspace(os.Getenv("WORKSPACE_ID"))
	if err != nil {
		panic(err)
	}
	service := workspace.SQL()

	page, err := service.QueryHistory(
		ctx,
		sdk.WithQueryHistoryOffset(0),
		sdk.WithQueryHistoryLimit(20),
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("Total:", page.GetTotal(), "Offset:", page.GetOffset(), "Limit:", page.GetLimit())
	for _, record := range page.GetQueryList() {
		fmt.Println(record.GetStatementId(), record.GetDatabase(), record.GetStatus(), record.GetError())
	}
}
```

::::

:::::

(sdk-ai-studio-sql-history-action-2)=
<span id="读取执行详情与画像"></span>

## Read execution details and profile

Select a target statement ID from the history list and reuse the workspace SQL service to fetch its execution details and profile. The detail response reports statement status and error text, while the profile response indicates whether profiling succeeded and returns the profile payload.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
if not page.query_list:
    raise SystemExit("No SQL execution records found in this workspace")

statement_id = page.query_list[0].statement_id
detail = service.query_detail(statement_id)
if detail is not None:
    print("Details:", detail.statement_id, detail.status, detail.error)

profile = service.query_profile(statement_id)
print("Profile:", profile.statement_id, profile.profile.success, profile.profile.message)
```

::::

::::{tab-item} Go
:sync: go

```go
if len(page.GetQueryList()) == 0 {
	fmt.Println("No SQL execution records found in this workspace")
	return
}

statementID := page.GetQueryList()[0].GetStatementId()
detail, err := service.QueryDetail(ctx, statementID)
if err != nil {
	panic(err)
}
if detail != nil {
	fmt.Println("Details:", detail.GetStatementId(), detail.GetStatus(), detail.GetError())
}

profile, err := service.QueryProfile(ctx, statementID)
if err != nil {
	panic(err)
}
fmt.Println("Profile:", profile.GetStatementId(), profile.GetProfile().GetSuccess(), profile.GetProfile().GetMessage())
```

::::

:::::

(sdk-ai-studio-sql-history-next)=
<span id="下一步"></span>

## Next steps

- [Run SQL Queries](../数据处理/执行SQL查询.md)
- [Inspect Task Execution Overview](查看任务执行概览.md)
