Hướng dẫn which is faster case or if in mysql? - trường hợp nào nhanh hơn hoặc nếu trong mysql?

Từ hướng dẫn, có vẻ như hàm if chỉ là một dạng ít linh hoạt hơn của biểu thức ____10. Ví dụ: bạn có thể viết:

select if[username = 'darxysaq', 'high', 'low'] as awesomeness

Và tương đương với

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
0:

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness

Nhưng

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
0 linh hoạt hơn. Nó cho phép nhiều hơn một nhánh, như:

select case 
       when username = 'darxysaq' then 'high' 
       when username = 'john skeet' then 'medium' 
       else 'low' 
       end as awesomeness

Và nó có thể hoạt động như một

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
3:

select case username 
       when 'darxysaq' then 'high' 
       when 'john skeet' then 'medium' 
       else 'low' 
       end as awesomeness

Bây giờ tuyên bố if là một con thú hoàn toàn khác. Đó là một tuyên bố kiểm soát trong các thủ tục MySQL. Mẫu câu lệnh trông giống như:

CREATE FUNCTION GetAwesomeness [username varchar[50]]
RETURNS varchar[20]
BEGIN
   IF username = 'darxysaq' THEN
      return 'high';
   ELSEIF username = 'john skeet' THEN
      return 'medium';
   ELSE
     return 'low';
   END IF;
END; //

Đây là một fiddle sql với phiên bản câu lệnh. Có vẻ như Mr Bean không phải là tất cả những gì anh ta tạo nên!

Một lưu ý cuối cùng: Biểu thức

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
0 là SQL tiêu chuẩn và hoạt động trong hầu hết các cơ sở dữ liệu. Hàm if không phải là SQL tiêu chuẩn và sẽ không hoạt động trong các cơ sở dữ liệu khác, như SQL Server hoặc PostgreSQL.

Tôi có truy vấn sau đây mất khoảng 20 giây để trả về dữ liệu:

    select
        landing_page,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2018' THEN all_impressions END] AS `imp [Aug-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='7' and year[dates]='2018' THEN all_impressions END] AS `imp [Jul-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2017' THEN all_impressions END] AS `imp [Aug-2017]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2018' THEN all_clicks END] AS `clk [Aug-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='7' and year[dates]='2018' THEN all_clicks END] AS `clk [Jul-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2017' THEN all_clicks END] AS `imp [Aug-2017]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2018' THEN all_ctr END] AS `clk [Aug-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='7' and year[dates]='2018' THEN all_ctr END] AS `clk [Jul-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2017' THEN all_ctr END] AS `imp [Aug-2017]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2018' THEN all_positions END] AS `clk [Aug-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='7' and year[dates]='2018' THEN all_positions END] AS `clk [Jul-2018]`,
        SUM[CASE WHEN profile_id=77 and month[dates]='8' and year[dates]='2017' THEN all_positions END] AS `imp [Aug-2017]`
    from
        landing_pages_v3
    where
        profile_id=77
    group by
        landing_page
    order by
        all_impressions desc
    limit 10

Bảng của tôi được cấu trúc như vậy:

+---------------------+---------------+------+-----+-------------------+-----------------------------+
| Field               | Type          | Null | Key | Default           | Extra                       |
+---------------------+---------------+------+-----+-------------------+-----------------------------+
| id                  | bigint[20]    | NO   | PRI | NULL              | auto_increment              |
| profile_id          | int[11]       | YES  | MUL | NULL              |                             |
| dates               | timestamp     | NO   | MUL | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| landing_page        | varchar[2083] | YES  |     | NULL              |                             |
| keyword_count       | int[11]       | YES  |     | NULL              |                             |
| all_impressions     | int[11]       | YES  |     | NULL              |                             |
| all_clicks          | int[11]       | YES  |     | NULL              |                             |
| all_ctr             | float         | YES  |     | NULL              |                             |
| all_positions       | float         | YES  |     | NULL              |                             |
| mobile_impressions  | int[11]       | YES  |     | NULL              |                             |
| mobile_clicks       | int[11]       | YES  |     | NULL              |                             |
| mobile_ctr          | float         | YES  |     | NULL              |                             |
| mobile_positions    | float         | YES  |     | NULL              |                             |
| tablet_impressions  | int[11]       | YES  |     | NULL              |                             |
| tablet_clicks       | int[11]       | YES  |     | NULL              |                             |
| tablet_ctr          | float         | YES  |     | NULL              |                             |
| tablet_positions    | float         | YES  |     | NULL              |                             |
| desktop_impressions | int[11]       | YES  |     | NULL              |                             |
| desktop_clicks      | int[11]       | YES  |     | NULL              |                             |
| desktop_ctr         | float         | YES  |     | NULL              |                             |
| desktop_positions   | float         | YES  |     | NULL              |                             |
+---------------------+---------------+------+-----+-------------------+-----------------------------+

Dữ liệu bảng khá dễ dàng chỉ là một URL cho cột

select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
7 và phần còn lại là
select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
8 hoặc
select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
9 [không bao gồm cột ngày tất nhiên].

Truy vấn này được sử dụng để tải một bảng để hiển thị dữ liệu cho người dùng, vì vậy, cần phải tải trong vòng 3 giây, lý tưởng.

Kích thước bảng hiện tại đang đóng cửa trên 15 triệu hàng.

Làm thế nào tôi có thể làm cho điều này nhanh hơn?

Tôi hy vọng có một truy vấn hoặc tối ưu hóa bảng khác mà tôi có thể làm - thay vào đó tôi có thể tổng hợp trước dữ liệu nhưng tôi muốn tránh điều đó.

Thông tin phiên bản:

+-------------------------+---------------------+
| Variable_name           | Value               |
+-------------------------+---------------------+
| innodb_version          | 5.5.53              |
| protocol_version        | 10                  |
| slave_type_conversions  |                     |
| version                 | 5.5.53-log          |
| version_comment         | Source distribution |
| version_compile_machine | x86_64              |
| version_compile_os      | Linux               |
+-------------------------+---------------------+

Cập nhật theo bình luận từ Akina, tôi đã có nó đến khoảng 6 giây [sau khi lưu trữ] với những điều sau: As per comments from Akina, I've got it to around 6 seconds [after caching] with the following:

        select
            landing_page,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2018' THEN all_impressions END] AS `imp [Aug-2018]`,
            SUM[CASE WHEN month[dates]='7' and year[dates]='2018' THEN all_impressions END] AS `imp [Jul-2018]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2017' THEN all_impressions END] AS `imp [Aug-2017]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2018' THEN all_clicks END] AS `clk [Aug-2018]`,
            SUM[CASE WHEN month[dates]='7' and year[dates]='2018' THEN all_clicks END] AS `clk [Jul-2018]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2017' THEN all_clicks END] AS `imp [Aug-2017]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2018' THEN all_ctr END] AS `clk [Aug-2018]`,
            SUM[CASE WHEN month[dates]='7' and year[dates]='2018' THEN all_ctr END] AS `clk [Jul-2018]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2017' THEN all_ctr END] AS `imp [Aug-2017]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2018' THEN all_positions END] AS `clk [Aug-2018]`,
            SUM[CASE WHEN month[dates]='7' and year[dates]='2018' THEN all_positions END] AS `clk [Jul-2018]`,
            SUM[CASE WHEN month[dates]='8' and year[dates]='2017' THEN all_positions END] AS `imp [Aug-2017]`
        from
            landing_pages_v3
        where
            profile_id=77 and month[dates] in ['7', '8'] and year[dates] in ['2017', '2018']
        group by
            landing_page
        order by
            all_impressions desc
        limit 10

Câu lệnh CASE có nhanh hơn nếu SQL khác?

Nói chung, các câu lệnh CASE có thể thực thi nhanh hơn, vì trình biên dịch hoặc thời gian chạy có thể xây dựng một bảng nhảy. Thông thường, với ít hơn năm mục, một trình biên dịch sẽ viết một câu lệnh CASE dưới dạng danh sách các câu lệnh IF/ELSE.case statements can execute faster, as the compiler or runtime can build a jump table. Usually, for less than five items, a compiler will write a case statement as a list of if/else statements.

Sự khác biệt giữa trường hợp và nếu trong SQL là gì?

Sự khác biệt là khá cơ bản.Câu lệnh IF rất hữu ích nếu bạn đang cố gắng đánh giá một cái gì đó với một điều kiện đúng/sai. Câu lệnh CASE được sử dụng khi bạn có nhiều điều kiện có thể.The IF statement is useful if you're trying to evaluate something to a TRUE/FALSE condition. The CASE statement is used when you have multiple possible conditions.

Bài Viết Liên Quan

Chủ Đề