C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)

一、基本要求

bash 复制代码
1.按需查询,不要的字段不查
2.缓存优先,内存比磁盘读写快(Redis也不要放太多的数据,按需存储,不然读写也慢)
3.线程并行,最后await输出所有结果
4.批量优先,插入或更新使用数据库批量操作
5.压缩算法,传输大量重复文本,避免网络大开销
6.耗时长操作推入消息队列,慢慢消费
7.循环内部短流程,不要for循环里面写耗时操作例如查库、读写文件
8.非必要不递归
9.非必要不加锁(尽量不要修改静态字段,尽量用实例字段)

二、Gzip算法

cs 复制代码
public class Program
{
    /*
        简介:Gzip是一种基于DEFLATE算法的无损数据压缩算法,广泛应用于文件压缩和网络数据传输。
        算法:DEFLATE 算法 =  LZ77字典编码 + 霍夫曼编码
        适用场景:大量重复内容文本、长文本
     */


    /// <summary>
    /// 压缩
    /// </summary>
    public static string CompressString(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        byte[] compressedData = null;
        using (var memoryStream = new MemoryStream())
        {
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
            }

            memoryStream.Position = 0;

            compressedData = new byte[memoryStream.Length];
            memoryStream.Read(compressedData, 0, compressedData.Length);
        }
        var gZipBuffer = new byte[compressedData.Length + 4];
        Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
        return Convert.ToBase64String(gZipBuffer);
    }

    /// <summary>
    /// 解压
    /// </summary>
    public static string DecompressString(string compressedText)
    {
        byte[] gZipBuffer = Convert.FromBase64String(compressedText);
        using (var memoryStream = new MemoryStream())
        {
            int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
            memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

            var buffer = new byte[dataLength];

            memoryStream.Position = 0;
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
            {
                gZipStream.Read(buffer, 0, buffer.Length);
            }

            return Encoding.UTF8.GetString(buffer);
        }
    }

    static async Task Main(string[] args)
    {
        // 测试1:高度重复的文本
        var repetitiveText = new string('A', 1000) + new string('B', 1000) + new string('C', 1000);
        Console.WriteLine($"重复文本原始长度: {repetitiveText.Length}");
        var compressed1 = CompressString(repetitiveText);
        Console.WriteLine($"重复文本压缩后: {compressed1.Length}");
        Console.WriteLine($"压缩比: {(double)compressed1.Length / repetitiveText.Length:P}");

        // 测试2:你的原始文本
        var originalText = ""ConsoleApp1.exe"(CoreCLR: clrhost): 已加载...";
        Console.WriteLine($"\n原始文本长度: {originalText.Length}");
        var compressed2 = CompressString(originalText);
        Console.WriteLine($"原始文本压缩后: {compressed2.Length}");
        Console.WriteLine($"压缩比: {(double)compressed2.Length / originalText.Length:P}");

        // 测试3:JSON数据(通常压缩效果好)
        var jsonText = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}".Repeat(50);
        Console.WriteLine($"\nJSON文本长度: {jsonText.Length}");
        var compressed3 = CompressString(jsonText);
        Console.WriteLine($"JSON文本压缩后: {compressed3.Length}");
        Console.WriteLine($"压缩比: {(double)compressed3.Length / jsonText.Length:P}");
    }
}

三、相关技巧

C#高级:提升数据库查询效率的优化策略与方法_c# 分表查询-CSDN博客

相关推荐
rockey6273 小时前
AScript如何实现中文脚本引擎
c#·.net·script·eval·expression·function·动态脚本
wuweijianlove4 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong4 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志4 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
lly2024064 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨4 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn9994 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏4944 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
黎阳之光5 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_115 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode