提升性能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档介绍了可用来提高应用性能的方法和技巧。在某些情况下,我们会采用其他 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
请求参数来指定您希望返回的字段。对于返回响应数据的任何请求,您都可以使用此参数。
示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):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"]]