提升效能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文說明提升應用程式成效的幾個技巧。在某些情況下,我們會使用其他 API 或通用 API 的範例來說明這些技巧背後的概念。不過,同樣的概念也適用於 Campaign Manager 360 API。
使用 gzip 壓縮
要減少每個要求占用的頻寬,最簡單的方法就是使用 gzip 壓縮檔。雖然此方法需要額外的 CPU 作業時間解壓縮,但相對可省下可觀的網路成本。
如果要接收以 gzip 編碼的回應,您必須執行下列兩項操作:設定 Accept-Encoding
標頭,並修改您的使用者代理程式來加入字串 gzip
。以下是一個啟用 gzip 壓縮的正確 HTTP 標頭格式範例:
Accept-Encoding: gzip
User-Agent: my program (gzip)
使用部分資源
另一種提高 API 呼叫效能的方式,就是只要求您想要的部分資料。這麼做可避免讓您的應用程式傳輸、剖析及儲存不需要的欄位,進而更有效地使用網路、CPU 及記憶體等資源。
部分回應
根據預設,伺服器會在處理要求後傳回完整的資源表示法。為改善成效,您可以要求伺服器只傳送您真正需要的欄位,並改為取得「部分回應」。
如果要請求部分回應,請使用 fields
要求參數來指定您想要傳回的欄位。您可以將此參數搭配任何會傳回回應資料的要求使用。
範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThis document outlines techniques for enhancing application performance, focusing on reducing bandwidth and resource usage.\u003c/p\u003e\n"],["\u003cp\u003eEnabling gzip compression, by setting specific HTTP headers, minimizes bandwidth consumption, despite requiring extra CPU time for decompression.\u003c/p\u003e\n"],["\u003cp\u003eOptimizing API calls by requesting only necessary data through partial responses, using the \u003ccode\u003efields\u003c/code\u003e parameter, improves network, CPU, and memory efficiency.\u003c/p\u003e\n"]]],[],null,["# Improve Performance\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 Campaign Manager 360 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"]]