通过雄辩的模型执行查询后,如何查看我的 mssql 服务器中运行的查询?

问题描述 投票:0回答:1

在我的 Laravel Tinker 会话中,我运行以下命令:

$records = MyModel::with('parent','children')->whereNull('parent_id')->get();

型号为:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

class MyModel extends Model
{

    protected $table='mymodel';

    public function setNameAttribute($name)
    {
        $name=htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
        $this->attributes['name']=$name;
    }
    public function parent(): HasOne
    {
        return $this->hasOne(Category::class,'id','parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

}

我想看看我在 Linux 机器上通过 docker-compose 运行的 mssql 服务器中实际执行了什么:

version: "3.3"
services:

  # PHP and nginx is here - ommited in order to keel question length sane

  mssql:
    # Replace with your own
    image: mcr.microsoft.com/mssql/server:2022-latest
    user: root
    networks:
      private:
      public:
        ipv4_address: ${IP_BASE}.3
    command: bash -c "chmod -R 777 /usr/src/sql/ && /opt/mssql/bin/sqlservr"
    volumes:
      - ./volumes/db/data:/var/opt/mssql/data
      - ./volumes/db/log:/var/opt/mssql/log
      - ./sql:/usr/src/sql
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: "<censored>"
      MSSQL_PID: "Developer"

在我的 mssql 工具 (DBeaver) 上运行:

CREATE EVENT SESSION QueryMonitor
ON SERVER
ADD EVENT sqlserver.sql_statement_completed
(ACTION(sqlserver.sql_text))
ADD TARGET package0.event_file
(SET filename='/var/opt/mssql/log/QueryMonitor.xel');

ALTER EVENT SESSION QueryMonitor ON SERVER STATE = START;

但是通过查看文件

QueryMonitor.xel
我只看到遥测记录:

SELECT target_data
                                    FROM sys.dm_xe_session_targets xet WITH(nolock)
                                    JOIN sys.dm_xe_sessions xes WITH(nolock)
                                    ON xes.address = xet.event_session_address
                                    WHERE xes.name = 'telemetry_xevents'
                                    AND xet.target_name = 'ring_buffer'$SELECT target_data
                                    FROM sys.dm_xe_session_targets xet WITH(nolock)
                                    JOIN sys.dm_xe_sessions xes WITH(nolock)
                                    ON xes.address = xet.event_session_address
                                    WHERE xes.name = 'telemetry_xevents'
                                    AND xet.target_name = 'ring_buffer'`t
��
  R.    �����d2�SET DEADLOCK_PRIORITY -104SET DEADLOCK_PRIORITY -10`t
��'R.   ��d�if not exists (select * from sys.dm_xe_sessions where name = 'telemetry_xevents') if not exists (select * from sys.dm_xe_sessions where name = 'telemetry_xevents')
    alter event session telemetry_xevents on server state=start`

而我期望看到的日志看起来像:

select * from mymodel ....

有什么想法为什么我看不到它们吗?

使用:

SELECT CAST(event_data AS XML) AS event_data FROM sys.fn_xe_file_target_read_file('/var/opt/mssql/log/QueryMonitor.xel', NULL, NULL, NULL);

我得到空结果集。

sql-server laravel docker logging
1个回答
0
投票

使用laravel日志代替数据库日志的方法:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        if(App::environment('local')){
            \DB::listen(function($query) {
                \Log::info(
                    $query->sql,
                    $query->bindings
                );
            });
        }
    }
}

使用它您可以在

./storage/logs/laravel.log
上看到运行日志。

© www.soinside.com 2019 - 2024. All rights reserved.