Cải thiện 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 các kỹ thuật mà bạn có thể sử dụng để cải thiện hiệu suất của ứng dụng.
Tài liệu cho API cụ thể mà bạn đang sử dụng phải có
trang tương tự với nhiều thông tin chi tiết hơn về một số chủ đề này.
Ví dụ: hãy xem
Trang Mẹo về hiệu suất cho API Google Drive.
Giới thiệu về gzip
Thư viện ứng dụng này yêu cầu nén gzip cho tất cả các phản hồi và giải nén API
dữ liệu cho bạn.
Mặc dù quá trình này đòi hỏi thêm thời gian của CPU để giải nén kết quả,
sự đánh đổi bằng chi phí mạng thường sẽ mang lại giá trị cho bạn.
Phản hồi một phần (tham số trường)
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 các 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,
thêm tham số Fields
tiêu chuẩn vào bất kỳ phương thức API nào.
Giá trị của tham số này chỉ định các trường mà bạn muốn 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.
Trong đoạn mã sau,
thì phương thức GetRest
của dịch vụ Khám phá sẽ được gọi.
Giá trị của tham số Fields
được đặt thành description,title
.
Do đó, đối tượng được trả về sẽ chỉ bao gồm các trường mô tả và tiêu đề.
var service = new DiscoveryService();
var request = service.Apis.GetRest("calendar", "v3");
request.Fields = "description,title";
var result = request.Execute();
Lưu ý cách sử dụng dấu phẩy để phân cách các trường mong muốn,
và dấu gạch chéo được dùng để cho biết các trường có trong các trường mẹ.
Có các tuỳ chọn định dạng khác cho tham số Fields
;
để biết chi tiết, hãy xem "Mẹo về hiệu suất" trang
trong tài liệu về API bạn đang sử dụng.
Cập nhật một phần (bản vá)
Nếu API mà bạn đang gọi có hỗ trợ bản vá,
bạn có thể tránh gửi dữ liệu không cần thiết khi sửa đổi tài nguyên.
Đối với các API này, bạn có thể gọi phương thức Patch
và
cung cấp các đối số bạn muốn sửa đổi cho tài nguyên.
Để biết thêm thông tin về ngữ nghĩa của bản vá,
hãy xem "Mẹo về hiệu suất" trong tài liệu về API mà bạn đang sử dụng.
Lô
Nếu đang gửi nhiều yêu cầu nhỏ, bạn có thể hưởng lợi từ
phân lô,
cho phép các yêu cầu đó được nhóm thành một yêu cầu HTTP duy nhất.
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\u003eThis document provides techniques to enhance your application's performance by reducing network costs and unnecessary data transfers.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003eFields\u003c/code\u003e parameter for partial responses, retrieving only the necessary data from the server.\u003c/p\u003e\n"],["\u003cp\u003eEmploy \u003ccode\u003ePatch\u003c/code\u003e for partial updates, sending only modified data when making changes to resources.\u003c/p\u003e\n"],["\u003cp\u003eLeverage batching to combine multiple small requests into a single HTTP request, reducing overhead.\u003c/p\u003e\n"],["\u003cp\u003eConsult the specific API documentation's "Performance Tips" page for detailed guidance on these techniques.\u003c/p\u003e\n"]]],[],null,["# Improve Performance\n\nThis document covers techniques you can use to improve the performance of your application.\nThe documentation for the specific API you are using should have a\nsimilar page with more detail on some of these topics.\nFor example, see the\n[Performance Tips page for the Google Drive API](/drive/performance).\n\nAbout gzip\n----------\n\n\nThis client library requests gzip compression for all API responses and unzips\nthe data for you.\nAlthough this requires additional CPU time to uncompress the results,\nthe tradeoff with network costs usually makes it worthwhile.\n\nPartial response (fields parameter)\n-----------------------------------\n\n\nBy default, the server sends back the full representation of a resource after processing requests.\nFor better performance,\nyou can ask the server to send only the fields you really need and get a *partial response* instead.\n\n\nTo request a partial response,\nadd the standard `Fields` parameter to any API method.\nThe value of this parameter specifies the fields you want returned.\nYou can use this parameter with any request that returns response data.\n\n\nIn the following code snippet,\nthe `GetRest` method of the Discovery service is called.\nThe value of the `Fields` parameter is set to `description,title`.\nAs a result, the returned object will include only the description and title fields. \n\n```gdscript\nvar service = new DiscoveryService();\nvar request = service.Apis.GetRest(\"calendar\", \"v3\");\nrequest.Fields = \"description,title\";\nvar result = request.Execute();\n \n```\n\n\nNote how commas are used to delimit the desired fields,\nand slashes are used to indicate fields that are contained in parent fields.\nThere are other formatting options for the `Fields` parameter;\nfor details, see the \"Performance Tips\" page\nin the documentation for the API you are using.\n\nPartial update (patch)\n----------------------\n\n\nIf the API you are calling supports patch,\nyou can avoid sending unnecessary data when modifying resources.\nFor these APIs, you can call the `Patch` method and\nsupply the arguments you wish to modify for the resource.\n\n\nFor more information about patch semantics,\nsee the \"Performance Tips\" page in the documentation for the API you are using.\n\nBatch\n-----\n\n\nIf you are sending many small requests you may benefit from\n[batching](/api-client-library/dotnet/guide/batch),\nwhich allows those requests to be bundled into a single HTTP request."]]