Mẹo tăng hiệu suất
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài liệu này bao gồm một số kỹ thuật mà bạn có thể sử dụng để cải thiện hiệu suất của ứng dụng. Trong một số trường hợp, ví dụ từ các API khác hoặc API chung được sử dụng để minh hoạ các ý tưởng được trình bày. Tuy nhiên, các khái niệm tương tự cũng có thể áp dụng cho API người mua Ad Exchange.
Nén bằng gzip
Một cách dễ dàng và thuận tiện để giảm băng thông cần thiết cho mỗi yêu cầu là bật chức năng nén gzip. Mặc dù điều này đòi hỏi thêm thời gian của CPU để giải nén kết quả, nhưng việc đánh đổi với chi phí mạng thường khiến việc đánh đổi bằng chi phí mạng thường rất đáng giá.
Để nhận phản hồi được mã hoá bằng gzip, bạn phải làm hai việc: Đặt tiêu đề Accept-Encoding
và sửa đổi tác nhân người dùng để chứa chuỗi gzip
. Dưới đây là ví dụ về tiêu đề HTTP được định dạng đúng cách để bật nén gzip:
Accept-Encoding: gzip
User-Agent: my program (gzip)
Xử lý các tài nguyên một phần
Một cách khác để cải thiện hiệu suất của lệnh gọi API là chỉ yêu cầu phần dữ liệu mà bạn quan tâm. Điều này giúp ứng dụng của bạn tránh được việc chuyển, phân tích cú pháp và lưu trữ các trường không cần thiết để có thể sử dụng các tài nguyên bao gồm mạng, CPU và bộ nhớ một cách hiệu quả hơn.
Phản hồi một phần
Theo mặc định, máy chủ gửi lại bản trình bày đầy đủ của một tài nguyên sau khi xử lý yêu cầu. Để có hiệu suất tốt hơn, bạn có thể yêu cầu máy chủ chỉ gửi những trường bạn thực sự cần và nhận phản hồi một phần.
Để yêu cầu phản hồi một phần, hãy sử dụng tham số yêu cầu fields
để chỉ định các trường bạn muốn được trả về. Bạn có thể sử dụng tham số này với bất kỳ yêu cầu nào trả về dữ liệu phản hồi.
Ví dụ:
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eEnabling gzip compression reduces bandwidth usage by compressing data, which often outweighs the cost of extra CPU time required for decompression.\u003c/p\u003e\n"],["\u003cp\u003eTo receive a gzip-encoded response, set the \u003ccode\u003eAccept-Encoding\u003c/code\u003e header to \u003ccode\u003egzip\u003c/code\u003e and include \u003ccode\u003egzip\u003c/code\u003e in your user agent string.\u003c/p\u003e\n"],["\u003cp\u003eRequesting partial resources, by only retrieving the data fields you need, improves application performance by conserving resources such as network, CPU, and memory.\u003c/p\u003e\n"],["\u003cp\u003ePartial responses are achieved by using the \u003ccode\u003efields\u003c/code\u003e request parameter to specify which fields you want returned from the server, instead of receiving the full resource representation.\u003c/p\u003e\n"]]],["To enhance application performance, enable gzip compression by setting the `Accept-Encoding: gzip` header and including `gzip` in the `User-Agent`. Additionally, request only necessary data by utilizing partial responses. Employ the `fields` request parameter to specify desired fields, thereby reducing data transfer, parsing, and storage overhead, optimizing network, CPU, and memory usage. These techniques can be used for the Ad Exchange Buyer API.\n"],null,["# Performance Tips\n\nThis document covers some techniques you can use to improve the performance of your application. In some cases, examples from other APIs or generic APIs are used to illustrate the ideas presented. However, the same concepts are applicable to the Ad Exchange Buyer API.\n\nCompression using gzip\n----------------------\n\nAn easy and convenient way to reduce the bandwidth needed for each request is to enable gzip compression. Although this requires additional CPU time to uncompress the results, the trade-off with network costs usually makes it very worthwhile.\n\nIn order to receive a gzip-encoded response you must do two things: Set an `Accept-Encoding` header, and modify your user agent to contain the string `gzip`. Here is an example of properly formed HTTP headers for enabling gzip compression: \n\n```perl6\nAccept-Encoding: gzip\nUser-Agent: my program (gzip)\n```\n\nWorking with partial resources\n------------------------------\n\nAnother way to improve the performance of your API calls is by requesting only the portion of the data that you're interested in. This lets your application avoid transferring, parsing, and storing unneeded fields, so it can use resources including network, CPU, and memory more efficiently.\n\n### Partial response\n\nBy default, the server sends back the full representation of a resource after processing requests. For better performance, you can ask the server to send only the fields you really need and get a *partial response* instead.\n\nTo request a partial response, use the `fields` request parameter to specify the fields you want returned. You can use this parameter with any request that returns response data.\n\n#### Example"]]