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

# كيفية تحديد الاستعلامات التي تستخدم العروض المادية في ClickHouse

> تعرّف على كيفية الاستعلام في سجلات ClickHouse لتحديد جميع الاستعلامات التي تتضمن عروضًا مادية ضمن نطاق زمني محدد.

***

<div id="question">
  ## السؤال
</div>

كيف أعرض جميع الاستعلامات التي تتعلق بالعروض المادية خلال آخر 60 دقيقة؟

<div id="answer">
  ## الإجابة
</div>

سيعرض هذا الاستعلام جميع الاستعلامات الموجَّهة إلى العروض المادية، مع الأخذ في الاعتبار ما يلي:

* يمكننا الاستفادة من الحقل `create_table_query` في جدول `system.tables` لتحديد الجداول التي تُعد الوجهة الصريحة (`TO`) للعروض المادية؛
* يمكننا أيضًا تتبّع الجداول التي تُعد الوجهة الضمنية للعروض المادية رجوعًا إلى المصدر (باستخدام `uuid` واصطلاح التسمية `.inner_id.<uuid>`)؛

ويمكننا أيضًا ضبط المدة الزمنية التي نريد الرجوع إليها بتغيير القيمة (افتراضيًا `60` دقيقة) في CTE للاستعلام الأولي

```sql theme={null}
WITH(60) -- default 60m
AS timeRange,
(
    --prepare names of possible implicit MV hidden target tables for *any* table with NON NULL uuid
    SELECT groupArray(
            concat('default.`.inner_id.', toString(uuid), '`')
        )
    FROM clusterAllReplicas(default, system.tables)
    WHERE notEmpty(uuid)
) AS MV_implicit_possible_hidden_target_tables_names_array,
(
    --captures MV name and target tables (if TO is specified)
    --TODO it seems that extract will return just the first capturing group :( replace with regexpExtract once available
    SELECT arrayFilter(
            x->x != '',
            --remove empty captures
            groupArray(
                extract(
                    create_table_query,
                    '^CREATE MATERIALIZED VIEW\s(\w+\.\w+)\s(?:TO\s(\S+))?'
                )
            )
        )
    FROM clusterAllReplicas(default, system.tables)
    WHERE engine = 'MaterializedView'
) AS MV_explicit_target_tables_names_array
SELECT event_time,
    query,
    tables as "MVs tables"
FROM clusterAllReplicas(default, system.query_log)
WHERE (
        -- only SELECT within 60m
        event_time > now() - toIntervalMinute(timeRange)
        AND startsWith(query, 'SELECT')
    ) -- check either that query involves implicit MV target table names
    AND (
        hasAny(
            tables,
            MV_implicit_possible_hidden_target_tables_names_array
        )
        OR -- check that query involves explicit MV target table
        hasAny(tables, MV_explicit_target_tables_names_array)
    )
ORDER BY event_time DESC;
```

المخرجات المتوقعة:

```sql theme={null}
| event_time          | query                                                                                          | MVs tables                                                            |
| ------------------- | ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| 2023-02-23 08:14:14 | SELECT     rand(),* FROM     default.sum_of_volumes,     default.big_changes,     system.users | ["default.big_changes_mv","default.sum_of_volumes_mv","system.users"] |
| 2023-02-23 08:04:47 | SELECT     price,* FROM     default.sum_of_volumes,     default.big_changes                    | ["default.big_changes_mv","default.sum_of_volumes_mv"]                |

```

في هذا المثال، يُعدّ كلٌّ من `default.big_changes_mv` و`default.sum_of_volumes_mv` عرضًا ماديًا.
