|
| 1 | +using ClickHouse.Driver.ADO; |
| 2 | +using ClickHouse.Driver.Utility; |
| 3 | + |
| 4 | +namespace ClickHouse.Driver.Examples; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Demonstrates how to use Query IDs to track and monitor query execution. |
| 8 | +/// Query IDs are useful for: |
| 9 | +/// - Debugging and troubleshooting specific queries |
| 10 | +/// - Tracking query execution in system.query_log |
| 11 | +/// - Correlating client-side operations with server-side logs |
| 12 | +/// - Monitoring query progress and performance |
| 13 | +/// </summary> |
| 14 | +public static class QueryIdUsage |
| 15 | +{ |
| 16 | + public static async Task Run() |
| 17 | + { |
| 18 | + using var connection = new ClickHouseConnection("Host=localhost"); |
| 19 | + await connection.OpenAsync(); |
| 20 | + |
| 21 | + Console.WriteLine("Query ID Usage Examples\n"); |
| 22 | + |
| 23 | + // Example 1: Automatic Query ID |
| 24 | + Console.WriteLine("1. Automatic Query ID assignment:"); |
| 25 | + await Example1_AutomaticQueryId(connection); |
| 26 | + |
| 27 | + // Example 2: Custom Query ID |
| 28 | + Console.WriteLine("\n2. Setting a custom Query ID:"); |
| 29 | + await Example2_CustomQueryId(connection); |
| 30 | + |
| 31 | + // Example 3: Tracking query execution |
| 32 | + Console.WriteLine("\n3. Tracking query execution in system.query_log:"); |
| 33 | + await Example3_TrackingQueryExecution(connection); |
| 34 | + |
| 35 | + // Example 4: Cancelling a query by Query ID |
| 36 | + Console.WriteLine("\n4. Query cancellation using Query ID:"); |
| 37 | + await Example4_QueryCancellation(connection); |
| 38 | + |
| 39 | + Console.WriteLine("\nAll Query ID examples completed!"); |
| 40 | + } |
| 41 | + |
| 42 | + private static async Task Example1_AutomaticQueryId(ClickHouseConnection connection) |
| 43 | + { |
| 44 | + // When you don't set a QueryId, ClickHouse automatically generates one |
| 45 | + using var command = connection.CreateCommand(); |
| 46 | + command.CommandText = "SELECT 'Hello from ClickHouse' AS message"; |
| 47 | + |
| 48 | + Console.WriteLine($" QueryId before execution: {command.QueryId ?? "(null)"}"); |
| 49 | + |
| 50 | + var result = await command.ExecuteScalarAsync(); |
| 51 | + |
| 52 | + // After execution, the QueryId is populated from the response headers |
| 53 | + Console.WriteLine($" QueryId after execution: {command.QueryId}"); |
| 54 | + Console.WriteLine($" Result: {result}"); |
| 55 | + } |
| 56 | + |
| 57 | + private static async Task Example2_CustomQueryId(ClickHouseConnection connection) |
| 58 | + { |
| 59 | + // You can set your own Query ID before executing a query |
| 60 | + // This is useful for correlation with your application logs |
| 61 | + var customQueryId = $"example-{Guid.NewGuid()}"; |
| 62 | + |
| 63 | + using var command = connection.CreateCommand(); |
| 64 | + command.CommandText = "SELECT version()"; |
| 65 | + command.QueryId = customQueryId; |
| 66 | + |
| 67 | + Console.WriteLine($" Custom QueryId: {customQueryId}"); |
| 68 | + |
| 69 | + var version = await command.ExecuteScalarAsync(); |
| 70 | + Console.WriteLine($" ClickHouse version: {version}"); |
| 71 | + Console.WriteLine($" QueryId remained: {command.QueryId}"); |
| 72 | + } |
| 73 | + |
| 74 | + private static async Task Example3_TrackingQueryExecution(ClickHouseConnection connection) |
| 75 | + { |
| 76 | + // Execute a query with a custom Query ID |
| 77 | + var trackableQueryId = $"trackable-{Guid.NewGuid()}"; |
| 78 | + using (var command = connection.CreateCommand()) |
| 79 | + { |
| 80 | + command.CommandText = $"SELECT 1"; |
| 81 | + command.QueryId = trackableQueryId; |
| 82 | + await command.ExecuteNonQueryAsync(); |
| 83 | + } |
| 84 | + |
| 85 | + Console.WriteLine($" Executed query with ID: {trackableQueryId}"); |
| 86 | + |
| 87 | + // Wait a moment for the query to be logged |
| 88 | + await Task.Delay(1000); |
| 89 | + |
| 90 | + // Query system.query_log to get information about our query |
| 91 | + // Note: system.query_log may need to be enabled in your ClickHouse configuration |
| 92 | + using (var command = connection.CreateCommand()) |
| 93 | + { |
| 94 | + command.CommandText = @" |
| 95 | + SELECT |
| 96 | + query_id, |
| 97 | + type, |
| 98 | + query_duration_ms, |
| 99 | + read_rows, |
| 100 | + written_rows, |
| 101 | + memory_usage |
| 102 | + FROM system.query_log |
| 103 | + WHERE query_id = {queryId:String} |
| 104 | + AND type = 'QueryFinish' |
| 105 | + ORDER BY event_time DESC |
| 106 | + LIMIT 1 |
| 107 | + "; |
| 108 | + command.AddParameter("queryId", trackableQueryId); |
| 109 | + |
| 110 | + try |
| 111 | + { |
| 112 | + using var reader = await command.ExecuteReaderAsync(); |
| 113 | + if (reader.Read()) |
| 114 | + { |
| 115 | + Console.WriteLine(" Query execution details from system.query_log:"); |
| 116 | + Console.WriteLine($" Query ID: {reader.GetString(0)}"); |
| 117 | + Console.WriteLine($" Type: {reader.GetString(1)}"); |
| 118 | + Console.WriteLine($" Duration: {reader.GetFieldValue<ulong>(2)} ms"); |
| 119 | + Console.WriteLine($" Rows read: {reader.GetFieldValue<ulong>(3)}"); |
| 120 | + Console.WriteLine($" Rows written: {reader.GetFieldValue<ulong>(4)}"); |
| 121 | + Console.WriteLine($" Memory usage: {reader.GetFieldValue<ulong>(5)} bytes"); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + Console.WriteLine(" (Query not yet in system.query_log - this table may have a delay or be disabled)"); |
| 126 | + } |
| 127 | + } |
| 128 | + catch (ClickHouseServerException ex) when (ex.ErrorCode == 60) |
| 129 | + { |
| 130 | + Console.WriteLine(" (system.query_log table not available on this server)"); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static async Task Example4_QueryCancellation(ClickHouseConnection connection) |
| 136 | + { |
| 137 | + // Demonstrate cancelling a long-running query using Query ID |
| 138 | + var cancellableQueryId = $"cancellable-{Guid.NewGuid()}"; |
| 139 | + |
| 140 | + Console.WriteLine($" Query ID: {cancellableQueryId}"); |
| 141 | + Console.WriteLine(" Starting a long-running query (SELECT sleep(5))..."); |
| 142 | + |
| 143 | + // Start the long-running query in a background task |
| 144 | + var queryTask = Task.Run(async () => |
| 145 | + { |
| 146 | + try |
| 147 | + { |
| 148 | + using var command = connection.CreateCommand(); |
| 149 | + command.CommandText = "SELECT sleep(3)"; |
| 150 | + command.QueryId = cancellableQueryId; |
| 151 | + |
| 152 | + await command.ExecuteScalarAsync(); |
| 153 | + Console.WriteLine(" Query completed (should have been cancelled)"); |
| 154 | + } |
| 155 | + catch (ClickHouseServerException ex) |
| 156 | + { |
| 157 | + // Query was killed on the server |
| 158 | + Console.WriteLine($" Server error: {ex.Message}"); |
| 159 | + } |
| 160 | + catch (Exception ex) |
| 161 | + { |
| 162 | + Console.WriteLine($" Query failed: {ex.Message}"); |
| 163 | + } |
| 164 | + }, CancellationToken.None); |
| 165 | + |
| 166 | + // Wait a bit for the query to start and be present in the log |
| 167 | + await Task.Delay(1000); |
| 168 | + |
| 169 | + // Cancel using KILL QUERY from another connection. Note that closing a connection will NOT kill any running queries opened by that connection. |
| 170 | + Console.WriteLine($" Cancelling query using KILL QUERY..."); |
| 171 | + try |
| 172 | + { |
| 173 | + // Create a separate connection for cancellation |
| 174 | + using var cancelConnection = new ClickHouseConnection("Host=localhost"); |
| 175 | + await cancelConnection.OpenAsync(); |
| 176 | + |
| 177 | + using var cancelCommand = cancelConnection.CreateCommand(); |
| 178 | + cancelCommand.CommandText = $"KILL QUERY WHERE query_id = '{cancellableQueryId}'"; |
| 179 | + await cancelCommand.ExecuteNonQueryAsync(); |
| 180 | + |
| 181 | + Console.WriteLine(" KILL QUERY command sent"); |
| 182 | + } |
| 183 | + catch (Exception ex) |
| 184 | + { |
| 185 | + Console.WriteLine($" Note: KILL QUERY failed (may require permissions): {ex.Message}"); |
| 186 | + } |
| 187 | + |
| 188 | + // Wait for the query task to complete |
| 189 | + await queryTask; |
| 190 | + } |
| 191 | +} |
0 commit comments