Current Location: Home> Latest Articles> Case Study of Using Attr_get in Database ORM Framework

Case Study of Using Attr_get in Database ORM Framework

M66 2025-05-22

In modern PHP application development, the ORM (Object Relational Mapping) framework has become the mainstream way to connect applications and databases. Frameworks such as Laravel's Eloquent, Doctrine, and RedBeanPHP have greatly simplified the complexity of database operations. However, the convenience of ORM is often accompanied by performance sacrifice, especially in high concurrency or large data volume scenarios, bottlenecks in underlying database connection and query performance gradually emerge.

This article will explore how to improve query performance with the mysqli_stmt::attr_get function in the ORM framework that uses native mysqli encapsulation, and realize real-time monitoring and optimization of execution status through reasonable encapsulation and usage strategies.

1. Understand the role of mysqli_stmt::attr_get

mysqli_stmt::attr_get is one of the methods of the mysqli_stmt object in PHP. Its function is to obtain the current attribute value of the prepared statement. Although the official documentation describes this function relatively briefly, it is very practical in performance debugging and underlying optimization.

Currently supported attributes mainly include:

  • MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH : Whether to update the maximum length of the column

  • MYSQLI_STMT_ATTR_CURSOR_TYPE : Cursor type (for example, forward cursor)

  • MYSQLI_STMT_ATTR_PREFETCH_ROWS : Number of pre-extracted rows (used to optimize result extraction performance)

2. Application scenarios in ORM

While most ORM frameworks encapsulate database interactions very deeply, if your ORM allows customization of the underlying database driver, or supports extending native mysqli_stmt , you can use attr_get to do some "snooping" and tuning.

Example: Check the cursor type of the current statement

In some high-performance scenarios, we may need to confirm whether the current preprocessing statement uses the appropriate cursor type. If scrollable cursor is enabled but is not actually needed, it will result in additional memory and resource usage.

 $stmt = $mysqli->prepare("SELECT * FROM logs WHERE created_at > ?");
$stmt->bind_param("s", $sinceTime);
$stmt->execute();

$cursorType = $stmt->attr_get(MYSQLI_STMT_ATTR_CURSOR_TYPE);
if ($cursorType !== MYSQLI_CURSOR_TYPE_NO_CURSOR) {
    error_log("warn:The current statement uses a non-default cursor,May affect performance");
}

You can embed such code into the "Query Monitor" or "Performance Diagnostic Module" of the ORM to dynamically check whether developers misuse high overhead settings.

3. Optimize with attribute settings

One of the best practices for mysqli_stmt::attr_get is to use it with attr_set . For example, in some cases you can set in advance that you do not need to update the maximum field length to speed up metadata processing.

 $stmt = $mysqli->prepare("SELECT description FROM products");
$stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, false);
$stmt->execute();

Then use attr_get to verify that the setup is successful:

 $maxLengthUpdate = $stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
if (!$maxLengthUpdate) {
    error_log("Field length update is disabled successfully,Improved performance");
}

4. Strategy for integrating attr_get in ORM

In order to better apply this optimization method to actual projects, we can consider adding the following logic to the "pre-execution hook" or "statement wrapper" of the ORM:

  1. Dynamically determine whether the query scenario requires complete field metadata .

  2. Automatically adjust statement properties according to statement type (for example, turn off maximum length update).

  3. After execution, use attr_get to check the settings results and record log or performance analysis information .

For example, in a custom ORM:

 class MyStatementWrapper {
    protected $stmt;

    public function __construct(mysqli_stmt $stmt) {
        $this->stmt = $stmt;
        $this->optimize();
    }

    protected function optimize() {
        $this->stmt->attr_set(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH, false);
    }

    public function execute() {
        $this->stmt->execute();
        $status = $this->stmt->attr_get(MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH);
        if (!$status) {
            PerformanceLogger::log("Field length update is disabled - " . date('Y-m-d H:i:s'));
        }
    }
}

In this way, while using ORM, you can also have performance control capabilities at the native driver level.

V. Conclusion

Although ORM greatly improves development efficiency, we still need to return to the "fundamentalist" approach in the dimension of performance optimization. mysqli_stmt::attr_get is a tool that many developers ignore, and it plays a unique role in diagnosis, tuning, and even anomaly detection.