MySQL Query

Tổng hợp một số lệnh Query MySQL thường sử dụng trong quá trình tương tác với cơ sở dữ liệu.

Index trường dữ liệu

Lệnh query đánh chỉ mục (index) trường dữ liệu. Cấu trúc query như sau:

ALTER TABLE table_name ADD INDEX your_index_name (column_name_1, column_name_2, column_name_3);

 

Ví dụ:

ALTER TABLE task_request_type_items ADD INDEX id_name_index (id, name);

Ví dụ trên được test đánh chỉ mục dữ liệu cho trường id và name của bảng task_request_type.

Query xem dung lượng database

SELECT 
    table_schema as `Database`, 
    ROUND(sum( data_length + index_length ) / 1024 / 1024,3) as `SizeInMB` 
FROM 
    information_schema.TABLES 
GROUP BY 
    table_schema
order by SizeInMB DESC

Query lấy lệnh tạo bảng

Query lấy cấu trúc lệnh query để tạo bảng cơ sở dữ liệu:

SHOW CREATE TABLE `table_name`

Query Truncate Table

Xoá toàn bộ dữ liệu bảng và reset khoá chính.

TRUNCATE TABLE `table_name`;
ALTER TABLE `table_name` AUTO_INCREMENT = 1;

Query lấy danh sách cột của bảng

Lấy danh sách cấu trúc cột của bảng hiển thị dạng table_name: field1, field2, field3,….

SELECT CONCAT(table_name, ': ', GROUP_CONCAT(column_name ORDER BY ordinal_position SEPARATOR ', '))
FROM information_schema.columns
WHERE table_name = 'task_request_type_items'
GROUP BY table_name

Kết quả ví dụ: task_request_type_items: id, parent_id, name, description, active

Query lấy cấu trúc theo định dạng text tuỳ chỉnh khác

SELECT CONCAT(table_name, ':\n', GROUP_CONCAT(CONCAT(column_name, ': [', column_type, CASE WHEN column_comment IS NOT NULL AND column_comment != '' THEN CONCAT(' - ', column_comment) ELSE '' END, ']\n') ORDER BY ordinal_position SEPARATOR ''))
FROM information_schema.columns
WHERE table_name = 'task_request_type_items'
GROUP BY table_name

Kết quả hiển thị ví dụ:

task_request_type_items:
id: [int(11)]
parent_id: [int(11) - Cha]
name: [varchar(100) - Tên công việc]
description: [text - Mô tả công việc]
active: [tinyint(1) - 0 - Không hoạt động | 1 - Hoạt động]

Query thống kê top bảng dữ liệu nhiều nhất

Thống kê top bảng dữ liệu có nhiều dòng dữ liệu nhất trong cơ sở dữ liệu và kích thước lưu trữ.

SELECT 
  TABLE_NAME, 
  FORMAT(TABLE_ROWS, 0) AS DATA_ROWS, 
CONCAT(FORMAT(DATA_LENGTH/1024/1024, 2), ' MB') AS TABLE_SIZE_MB
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'quanly_qlmoi'
ORDER BY TABLE_ROWS DESC
LIMIT 20;

Kết quả hiển thị ví dụ:

warehouse_tmp_details: 1,448,606 size: 95.61 MB
warehouse_transaction_details: 1,407,701 size: 86.61 MB
pancake_tags_detail: 928,949 size: 57.58 MB
action_histories: 696,228 size: 70.59 MB
love_newyear_event_histories: 427,476 size: 26.55 MB
rates: 395,660 size: 18.55 MB
voucher_detail: 353,232 size: 48.58 MB
love_transactions: 267,552 size: 28.56 MB
ci_sessions: 260,448 size: 63.34 MB
appointments: 208,596 size: 36.56 MB
admin_user_schedule_detail: 188,307 size: 7.52 MB
sale_care_phone_logs: 153,604 size: 85.61 MB
academy_student_care: 144,202 size: 10.52 MB
ads_recalls: 141,009 size: 37.56 MB
customer_complain_users: 128,847 size: 5.52 MB
sale_care_phone_camps: 95,618 size: 3.52 MB
aca_sale_care_list: 94,325 size: 6.52 MB
warehouse_transaction_logs: 92,165 size: 8.52 MB
sale_care_list: 80,975 size: 6.52 MB
sale_care_offline_phone_camps: 72,708 size: 2.52 MB

 

 

Trang Chủ
Dịch Vụ
Chat Live
Danh Mục
Hotline
×