› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
funbox
V2EX  ›  MySQL

日常涨知识系列-惊天地泣鬼神的 sql, 300 行~~

  •  3
     
  •   funbox · Dec 23, 2020 · 17318 views
    This topic created in 2151 days ago, the information mentioned may be changed or developed.

    没有别的意思就是想让大家,长长见识~ 能看懂算我输

    SELECT
    	* 
    FROM
    	(
    	SELECT
    		`inventory`.`id` AS `inventoryId`,
    		`inventory`.`location_id` AS `locationId`,
    		`inventory`.`storehouse_id` AS `storehouseId`,
    		`product`.`id` AS `productId`,
    		`product`.`name` AS `name`,
    		`product`.`number` AS `number`,
    		`brand`.`name` AS `brandName`,
    		`product`.`size` AS `size`,
    		`category`.`name` AS `categoryName`,
    		`product`.`unit` AS `unit`,
    		`storehouse`.`name` AS `storehouseName`,
    		`location`.`name` AS `locationName`,
    		IFNULL( inventory.quantity, 0 ) AS `remainQuantity`,
    		IFNULL( inventoryBath.avgPrice, 0 ) AS `avgPrice`,
    		IFNULL( inventoryBath.totalPrice, 0 ) AS `totalPrice`,
    		`inventoryWarning`.`lowerLimit` AS `lowerLimit`,
    		`inventoryWarning`.`upperLimit` AS `upperLimit`,(
    			IFNULL( repairOrderItem.repairRemainQuantity, 0 )+ IFNULL( purchaseReturnOrderItem.purchaseReturnRemainQuantity, 0 )+ IFNULL( otherOutOrderItem.otherInOutstockQuantity, 0 )) AS `waitOutQuantity`,(
    		IFNULL( purchaseOrderItem.purchaseRemainQuantity, 0 )+ IFNULL( otherInOrderItem.otherInOutstockQuantity, 0 )+ IFNULL( onlineInOrderItem.onlineRemainInstockQuantity, 0 )) AS `waitIntQuantity`,
    		`product`.`barcode` AS `barcode`,
    		`supplier`.`name` AS `supplierName`,
    		`product`.`valid_period` AS `validPeriod`,
    		`product`.`standard_id` AS `standardId`,
    		`product`.`from_id` AS `fromId`,
    		`product`.`source` AS `source`,
    		`oeTable`.`oeNumber` AS `oeNumber`,
    		`product`.`vehicle_note` AS `vehicleNote`,
    		`product`.`description` AS `note`,
    		`store`.`name` AS `storeName`,
    		`product`.`create_time` AS `createTime`,
    		`inventory`.`tenant_id`,
    		`product`.`sale_price1` AS `productSalePrice`,
    		`product`.`price_mode` AS `productPriceMode`,
    		`product`.`markup_type` AS `productMarkupType`,
    		IFNULL( lastPurchasePrice.lastPurchasePrice, 0 ) AS `lastPurchasePrice`,(
    		CASE
    				
    				WHEN product.price_mode = 1 THEN
    				product.sale_price1 
    				WHEN product.price_mode = 2 
    				AND product.markup_type = 1 
    				AND IFNULL( inventory.quantity, 0 )= 0 THEN
    					IFNULL( lastOutPrice.unitPrice, 0 )* product.sale_price1 
    					WHEN product.price_mode = 2 
    					AND product.markup_type = 1 
    					AND IFNULL( inventory.quantity, 0 ) != 0 THEN
    						IFNULL( inventoryBath.avgPrice, 0 )* product.sale_price1 
    						WHEN product.price_mode = 2 
    						AND product.markup_type != 1 THEN
    							IFNULL( lastPurchasePrice.lastPurchasePrice, 0 )* product.sale_price1 
    						END 
    						) AS salePrice 
    					FROM
    						`inventory`
    						LEFT JOIN `product` ON `inventory`.`product_id` = `product`.`id`
    						LEFT JOIN `category` ON `product`.`category_id` = `category`.`id`
    						LEFT JOIN `brand` ON `product`.`brand_id` = `brand`.`id`
    						LEFT JOIN `storehouse` ON `inventory`.`storehouse_id` = `storehouse`.`id`
    						LEFT JOIN `location` ON `inventory`.`location_id` = `location`.`id`
    						LEFT JOIN `supplier` ON `product`.`supplier_id` = `supplier`.`id`
    						LEFT JOIN `store` ON `inventory`.`store_id` = `store`.`id`
    						LEFT JOIN (
    						SELECT
    							SUBSTRING_INDEX(
    								GROUP_CONCAT( unit_price ORDER BY outstock_time DESC ),
    								',',
    								1 
    							) AS `unitPrice`,
    							`product_id` AS `productId` 
    						FROM
    							`inventory_batch` 
    						WHERE
    							( `store_id` = '27358' ) 
    							AND ( `inventory_batch`.`tenant_id` = 1 ) 
    						GROUP BY
    							`product_id` 
    						) `lastOutPrice` ON `inventory`.`product_id` = lastOutPrice.productId
    						LEFT JOIN (
    						SELECT
    							group_concat( product_number.number SEPARATOR ";" ) AS `oeNumber`,
    						`product_number`.`product_id` AS `productId` 
    						FROM
    							`product_number` 
    						WHERE
    							( `product_number`.`type` = 1 ) 
    							AND ( `product_number`.`tenant_id` = 1 ) 
    						GROUP BY
    							`product_number`.`product_id` 
    						) `oeTable` ON `inventory`.`product_id` = oeTable.productId
    						LEFT JOIN (
    						SELECT
    							`storehouse_id` AS `storehouseId`,
    							SUM( quantity )- SUM( outstock_quantity ) AS `remainQuantity`,
    							SUM(
    								unit_price *(
    									quantity - outstock_quantity 
    								)) AS `totalPrice`,
    							`product_id` AS `productId`,
    						IF
    							(
    								inventoryBathAll.currentQuantity = 0,
    								inventoryBathLast.unitPrice,
    							round( inventoryBathAll.avgPrice, 2 )) AS `avgPrice` 
    						FROM
    							`inventory_batch`
    							LEFT JOIN (
    							SELECT
    								`product_id` AS `productId`,
    								SUM( quantity )- SUM( outstock_quantity ) AS `currentQuantity`,
    								CONVERT (
    									SUM(
    										unit_price *(
    											quantity - outstock_quantity 
    										))/(
    									SUM( quantity )- SUM( outstock_quantity )),
    								DECIMAL ( 20, 2 )) AS `avgPrice` 
    							FROM
    								`inventory_batch` 
    							WHERE
    								( `store_id` = '27358' ) 
    								AND ( `inventory_batch`.`tenant_id` = 1 ) 
    							GROUP BY
    								`product_id` 
    							) `inventoryBathAll` ON `inventory_batch`.`product_id` = inventoryBathAll.productId
    							LEFT JOIN (
    							SELECT
    								SUBSTRING_INDEX(
    									GROUP_CONCAT( unit_price ORDER BY batch_number DESC ),
    									",",
    									1 
    								) AS `unitPrice`,
    								`product_id` AS `productId`,
    								max( batch_number ) AS `batchNumber` 
    							FROM
    								`inventory_batch` 
    							WHERE
    								( `store_id` = '27358' ) 
    								AND ( `inventory_batch`.`tenant_id` = 1 ) 
    							GROUP BY
    								`product_id` 
    							ORDER BY
    								`batch_number` DESC 
    							) `inventoryBathLast` ON `inventory_batch`.`product_id` = inventoryBathLast.productId 
    						WHERE
    							( `store_id` = '27358' ) 
    							AND ( `inventory_batch`.`tenant_id` = 1 ) 
    						GROUP BY
    							`product_id`,
    							`storehouse_id` 
    						) `inventoryBath` ON ( `inventory`.`product_id` = inventoryBath.productId ) 
    						AND ( `inventory`.`storehouse_id` = inventoryBath.storehouseId )
    						LEFT JOIN (
    						SELECT
    							`product_id` AS `productId`,
    							`lower_limit` AS `lowerLimit`,
    							`upper_limit` AS `upperLimit` 
    						FROM
    							`inventory_warning` 
    						WHERE
    							( `store_id` = '27358' ) 
    						AND ( `inventory_warning`.`tenant_id` = 1 )) `inventoryWarning` ON `inventory`.`product_id` = inventoryWarning.productId
    						LEFT JOIN (
    						SELECT
    							SUM( repair_order_product_item.quantity - repair_order_product_item.use_quantity ) AS `repairRemainQuantity`,
    							`repair_order_product_item`.`product_id` AS `productId` 
    						FROM
    							`repair_order_product_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`repair_order` 
    							WHERE
    								( `status` != 21000 ) 
    								AND ( `repair_order`.`store_id` = '27358' ) 
    							AND ( `repair_order`.`tenant_id` = 1 )) `repairOrder` ON `repair_order_product_item`.`order_id` = repairOrder.id
    							INNER JOIN (
    							SELECT DISTINCT
    								`product_id` 
    							FROM
    								`inventory` 
    							WHERE
    								( `store_id` = '27358' ) 
    								AND ( `inventory`.`tenant_id` = 1 ) 
    							GROUP BY
    								`product_id` 
    							) `inventoryQue` ON `repair_order_product_item`.`product_id` = inventoryQue.product_id 
    						WHERE
    							`repair_order_product_item`.`tenant_id` = 1 
    						GROUP BY
    							`repair_order_product_item`.`product_id` 
    						) `repairOrderItem` ON `inventory`.`product_id` = repairOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							SUM( purchase_return_order_item.quantity - purchase_return_order_item.outstock_quantity ) AS `purchaseReturnRemainQuantity`,
    							`purchase_return_order_item`.`product_id` AS `productId` 
    						FROM
    							`purchase_return_order_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`purchase_return_order` 
    							WHERE
    								(
    								`purchase_return_order`.`status` IN ( 12000, 13000 )) 
    								AND ( `purchase_return_order`.`store_id` = '27358' ) 
    							AND ( `purchase_return_order`.`tenant_id` = 1 )) `purchaseReturnOrder` ON `purchase_return_order_item`.`order_id` = purchaseReturnOrder.id 
    						WHERE
    							`purchase_return_order_item`.`tenant_id` = 1 
    						GROUP BY
    							`purchase_return_order_item`.`product_id` 
    						) `purchaseReturnOrderItem` ON `inventory`.`product_id` = purchaseReturnOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							SUM( other_in_out_stock_order_item.quantity ) AS `otherInOutstockQuantity`,
    							`other_in_out_stock_order_item`.`product_id` AS `productId` 
    						FROM
    							`other_in_out_stock_order_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`other_in_out_stock_order` 
    							WHERE
    								( `other_in_out_stock_order`.`type` = 1 ) 
    								AND ( `other_in_out_stock_order`.`status` = 1 ) 
    								AND ( `other_in_out_stock_order`.`store_id` = '27358' ) 
    							AND ( `other_in_out_stock_order`.`tenant_id` = 1 )) `otherInOutOrder` ON `other_in_out_stock_order_item`.`order_id` = otherInOutOrder.id 
    						WHERE
    							`other_in_out_stock_order_item`.`tenant_id` = 1 
    						GROUP BY
    							`other_in_out_stock_order_item`.`product_id` 
    						) `otherOutOrderItem` ON `inventory`.`product_id` = otherOutOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							SUM( purchase_order_item.quantity - purchase_order_item.instock_quantity ) AS `purchaseRemainQuantity`,
    							`purchase_order_item`.`product_id` AS `productId` 
    						FROM
    							`purchase_order_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`purchase_order` 
    							WHERE
    								(
    								`purchase_order`.`status` IN ( 12000, 13000 )) 
    								AND ( `purchase_order`.`store_id` = '27358' ) 
    							AND ( `purchase_order`.`tenant_id` = 1 )) `purchaseOrder` ON `purchase_order_item`.`order_id` = purchaseOrder.id 
    						WHERE
    							`purchase_order_item`.`tenant_id` = 1 
    						GROUP BY
    							`purchase_order_item`.`product_id` 
    						) `purchaseOrderItem` ON `inventory`.`product_id` = purchaseOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							SUM( other_in_out_stock_order_item.quantity ) AS `otherInOutstockQuantity`,
    							`other_in_out_stock_order_item`.`product_id` AS `productId` 
    						FROM
    							`other_in_out_stock_order_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`other_in_out_stock_order` 
    							WHERE
    								( `other_in_out_stock_order`.`type` = 2 ) 
    								AND ( `other_in_out_stock_order`.`status` = 1 ) 
    								AND ( `other_in_out_stock_order`.`store_id` = '27358' ) 
    							AND ( `other_in_out_stock_order`.`tenant_id` = 1 )) `otherInOutOrder` ON `other_in_out_stock_order_item`.`order_id` = otherInOutOrder.id 
    						WHERE
    							`other_in_out_stock_order_item`.`tenant_id` = 1 
    						GROUP BY
    							`other_in_out_stock_order_item`.`product_id` 
    						) `otherInOrderItem` ON `inventory`.`product_id` = otherInOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							SUM( online_purchase_order_item.quantity - online_purchase_order_item.instock_quantity ) AS `onlineRemainInstockQuantity`,
    							`online_purchase_order_item`.`product_id` AS `productId` 
    						FROM
    							`online_purchase_order_item`
    							INNER JOIN (
    							SELECT
    								* 
    							FROM
    								`online_purchase_order` 
    							WHERE
    								( `online_purchase_order`.`status` = 1 ) 
    								AND ( `online_purchase_order`.`store_id` = '27358' ) 
    							AND ( `online_purchase_order`.`tenant_id` = 1 )) `onlineOrder` ON `online_purchase_order_item`.`order_id` = onlineOrder.id 
    						WHERE
    							`online_purchase_order_item`.`tenant_id` = 1 
    						GROUP BY
    							`online_purchase_order_item`.`product_id` 
    						) `onlineInOrderItem` ON `inventory`.`product_id` = onlineInOrderItem.productId
    						LEFT JOIN (
    						SELECT
    							`product_id` AS `productId`,
    							`last_price` AS `lastPurchasePrice` 
    						FROM
    							`purchase_price` 
    						WHERE
    							( `store_id` = '27358' ) 
    						AND ( `purchase_price`.`tenant_id` = 1 )) `lastPurchasePrice` ON `inventory`.`product_id` = lastPurchasePrice.productId 
    					WHERE
    						( `product`.`status` != 90 ) 
    						AND ( `inventory`.`store_id` = '27358' ) 
    					AND ( `inventory`.`tenant_id` = 1 )) `table` 
    				WHERE
    					`table`.`tenant_id` = 1 
    				ORDER BY
    				`createTime` DESC 
    	LIMIT 10
    
    Supplement 1  ·  Dec 24, 2020
    主要是这个 sql 是 mysql 慢日志里面搜出来的
    业务是前端界面 用户可以任意点击。。。。
    109 replies  •  2020-12-25 16:51:06 +08:00
    1  2  
    ❮ ❯
    ttys001
    ttys001
    Dec 23, 2020❤️1
    1
    sql 不用维护吧,不用吧,不用吧……
    funbox
    funbox
    OP
    Dec 23, 2020
    2
    @ttys001 最近优化碰到的,生产环境!!!
    hack2012
    hack2012
    Dec 23, 2020
    3
    这个估计是 dba 写的。
    MIUIOS
    MIUIOS
    Dec 23, 2020❤️5
    4
    ........写这种 SQL 的也是人才 好家伙直接在 sql 里写业务逻辑
    ben1024
    ben1024
    Dec 23, 2020
    5
    emm... 能说正常操作吗,在处理数据聚合时根本不知道会写多长的 SQL
    melvin
    melvin
    Dec 23, 2020
    6
    真特么人才 几秒输出结果
    cslive
    cslive
    Dec 23, 2020
    7
    见多了,都是从数仓抽数的
    kiracyan
    kiracyan
    Dec 23, 2020
    8
    虽然是屎山 但是整理好应该能看懂的
    learningman
    learningman
    Dec 23, 2020❤️4
    9
    不错了,都是英文,结合业务看看就懂
    如果把以上都换成拼音首字母,你直接跳楼更干脆些
    lovecy
    lovecy
    Dec 23, 2020
    10
    其实结构还挺简单的,LEFT JOIN 一堆表,然后从生成的临时表用 WHERE 抽数,看是能看懂,改起来就痛苦了,最好祈祷不要出问题。。。
    czzt1
    czzt1
    Dec 23, 2020❤️1
    11
    这才几行,几千行的存储过程写的业务逻辑见过么
    acr0ss
    acr0ss
    Dec 23, 2020
    12
    结构清晰的,但是理解逻辑就难了。
    好在每个 join structure 的 table alias 起名挺用心,意义很明确;在没有注释的情况下,能给出不少提示。
    securityCoding
    securityCoding
    Dec 23, 2020
    13
    @melvin 应该是在数仓跑的
    joesonw
    joesonw
    Dec 23, 2020❤️1
    14
    BI 里面不都这么写的吗? 还是这个是业务环境
    funbox
    funbox
    OP
    Dec 23, 2020
    15
    @cslive 主要是用户前端点击列表的 sql 。。。。。
    tojonozomilk
    tojonozomilk
    Dec 23, 2020
    16
    什么?居然有这么简单的 SQL ?——来自数仓开发
    huobazi
    huobazi
    Dec 23, 2020
    17
    几千行的 sp,sp 套 sp,各种传参传 xml 和 json,还不是一个字 ”整“

    这种 sql 都是 先写个 小的,简单的,慢慢加上去的。其实也没啥。

    好的代码都是 helloword,但 helloword 不值钱
    lllllIIIlll
    lllllIIIlll
    Dec 23, 2020
    18
    数据分析那边几百行的 sql 太多了
    taogen
    taogen
    Dec 23, 2020
    19
    某列表页面 SQL,join 20 个,卒
    w292614191
    w292614191
    Dec 23, 2020
    20
    你这个一般般,我们这 C#项目,几百行几百行是基本写法。
    hafuhafu
    hafuhafu
    Dec 23, 2020
    21
    其实还好,我们这里抽取报表数据蛮多很长的。
    FlyingShark
    FlyingShark
    Dec 23, 2020
    22
    这个还好,结构还算清楚、而且别名 alias 容易阅读。也见过 700 行的 SQL,一堆 union all 拼起来……
    roundgis
    roundgis
    Dec 23, 2020 via Android
    23
    @czzt1 前東家的系統是兩層架構,客戶端直連數據庫,核心代碼就是那堆存儲過程,隨便一個函數都過萬行代碼
    fhsan
    fhsan
    Dec 23, 2020
    24
    @roundgis 好家伙,台湾公司有钱买了两份 oracle,同步数据、处理数据等等都是 sql 干,要不买了干嘛
    andyskaura
    andyskaura
    Dec 23, 2020
    25
    我们整个项目就是这么开发的 这样的确不好 但是要改吧 快 20 年都这么过来了 没必要
    manami
    manami
    Dec 23, 2020
    26
    你是没见过上千行的一个存储过程
    fanfpy
    fanfpy
    Dec 23, 2020
    27
    存储过程不都这长度吗
    fanfpy
    fanfpy
    Dec 23, 2020
    28
    ![1073bfd22008d0b5439b890c3ca1755.png]( https://i.loli.net/2020/12/23/XL9ERHbihIT6sAB.png)
    526326991
    526326991
    Dec 23, 2020
    29
    做过数据报表表示 正常操作~~~
    fish267
    fish267
    Dec 23, 2020❤️6
    30
    这种贴数仓代码,不是违规么,要有程序员基本素养哈
    thtznet
    thtznet
    Dec 23, 2020
    31
    楼主少见多怪,这段 sql 脚本不是 erp 日常么?楼主估计没有从事过企业内信息系统的开发。
    251243021
    251243021
    Dec 23, 2020
    32
    请问这么多行都是怎么读懂且保证修改不出问题的呢
    todd7zhang
    todd7zhang
    Dec 23, 2020
    33
    ERP 里面满天飞
    antiquezzz
    antiquezzz
    Dec 23, 2020
    34
    看着还是挺整齐的,sql 写的挺好的,这样效率高
    xpresslink
    xpresslink
    Dec 23, 2020
    35
    其实这东西难度没多大,只是需要充分理解业务和数据的含义。
    这种东西就象垃圾堆经年累月堆得太高了以后,让人有一触即溃的感觉而已。
    annielong
    annielong
    Dec 23, 2020
    36
    erp 业务类基本操作,只有 cms 类的不怎么用这些
    g76
    g76
    Dec 23, 2020
    37
    长见识了
    potatoyam
    potatoyam
    Dec 23, 2020
    38
    好熟悉看着像理财业务的统计 sql
    a54552239
    a54552239
    Dec 23, 2020
    39
    数据量大了,不会卡吗
    lcdxiangzi
    lcdxiangzi
    Dec 23, 2020
    40
    如果去掉最后一行,会有什么效果?
    Erskine
    Erskine
    Dec 23, 2020
    41
    嵌套多层,可以用 with as 把水平代码拆分成垂直,更好理解。
    lap510200
    lap510200
    Dec 23, 2020
    42
    联了很多张表 嵌套子查询 临时表 效率低下 但是写的人也蛮苦了 至少根据理清业务逻辑写出来这些 sql 加上调试这打断 sql 是否满足需求 这些所花费的时间估计至少 1 天

    除此之外把技术总监拖出去毙了 居然没有代码审核
    sampeng
    sampeng
    Dec 23, 2020
    43
    才 300 行。。见过几千行的么?弟弟。。。
    kisshere
    kisshere
    Dec 23, 2020
    44
    MySQL:你是不是觉得你很幽默?
    Yooloo
    Yooloo
    Dec 23, 2020
    45
    我只看到了 27358
    quan01994
    quan01994
    Dec 23, 2020
    46
    还好啦
    IrisFrankie
    IrisFrankie
    Dec 23, 2020
    47
    挺整齐的,命名也没啥问题。你理解需求后再读应该会挺简单的。。。不要试图否定它,除非你能优化它
    unicloud
    unicloud
    Dec 23, 2020
    48
    打扰了。。。
    DarkCat123
    DarkCat123
    PRO
    Dec 23, 2020
    49
    这是人类文明的精华。
    OldHu
    OldHu
    Dec 23, 2020❤️4
    50
    见过 sql 保存成文本文件有 2MB 的吗? 你们啊,too naive !

    ERP 系统啥的,长 sql 还是很多的。 所以,ERP 系统大部分还是 Oracle 这种商业数据库多。
    MySQL 是借着互联网起来的。业务相对简单,开发人员多,很多逻辑用开发代码解决,而不是用数据库。

    我以前做 Oracle ERP 的很多业务开发,就是 plsql + shell, 就跟现在后端写 java 一样。

    v2ex 大部分都是互联网从业者为主,还有很多朋友是前端,所以对这样有点 old style 的东西很陌生,陌生了又容易导致反感。
    limuyan44
    limuyan44
    Dec 23, 2020❤️1
    51
    才 300 行而已,刚毕业那会写了半年 sql,300 行算是起步了。
    la2la
    la2la
    Dec 23, 2020
    52
    @OldHu 对的,oracle erp 系统,包括之前的一些报表系统,300 行基本操作
    Vegetable
    Vegetable
    Dec 23, 2020
    53
    这个虽然长,但是逻辑不复杂吧,只是字段多而已。
    mengdodo
    mengdodo
    Dec 23, 2020
    54
    牛皮
    yungo8
    yungo8
    Dec 23, 2020 via Android
    55
    感觉 erp 报表常见,反正数仓数据处理的我是见过写很多行的
    optional
    optional
    Dec 23, 2020 via iPhone
    56
    不说 erp,搞数据分析也觉得这个没什么啊
    Mohanson
    Mohanson
    Dec 23, 2020
    57
    哈哈, 楼主本来想带节奏, 结果楼下一堆人才把楼主描述成一个"没见识的"...
    auin
    auin
    Dec 23, 2020
    58
    PL/SQL:行数算什么,调用栈堪比 Java
    mamahaha
    mamahaha
    Dec 23, 2020
    59
    这是不想让人抢饭碗吧?升级业务老老实实还得找他。
    Umenezumi
    Umenezumi
    Dec 23, 2020❤️1
    60
    数据分析那边儿经常是这种 SQL 啊,这个还算逻辑清晰了
    jasonkayzk
    jasonkayzk
    Dec 23, 2020
    61
    想知道这种 SQL 的性能如何= =
    Felldeadbird
    Felldeadbird
    Dec 23, 2020
    62
    这 SQL 写得很好啊。至少说明 DB 命名上 规范。如果楼主遇到过一推 AS a, b ,c, d...

    我很好奇这代码性能。。。

    最后,楼主直接贴代码,不怕问责吗?
    AntoniotheFuture
    AntoniotheFuture
    Dec 23, 2020
    63
    小意思啦,我之前一个项目一条 SQL3000 行而且有 4 条,跑一个小时,后来我亚索成 1000 行了,运行时间降到 20 分钟
    thetbw
    thetbw
    Dec 23, 2020 via Android
    64
    还好,我们公司 4,5 百行的也有,还是一个 sql 联接 10 多张表的,加上子查询。只能说公司服务器真的强大,还有就是访问量也没有很大,接口速度还能接受
    ZhaoHongXuan
    ZhaoHongXuan
    Dec 23, 2020
    65
    odps 上跑的?
    zm8m93Q1e5otOC69
    zm8m93Q1e5otOC69
    Dec 23, 2020 via Android
    66
    @roundgis 青铜器我记得就是这样的好像
    kevinjaz
    kevinjaz
    Dec 23, 2020
    67
    我觉得还好呀
    fatestigma
    fatestigma
    Dec 23, 2020
    68
    让我想起了当年上数据库课,几个加分题都是这种上百行的,写完之后过一天就看不懂自己写的啥了
    FieldFarmer
    FieldFarmer
    Dec 23, 2020 via Android
    69
    很多项目直接把业务逻辑写在 oracle 里面的…用存储过程
    Tink
    Tink
    Dec 24, 2020 via Android
    70
    这不是手写的,这绝壁不是
    loading
    loading
    Dec 24, 2020 via Android
    71
    看着挺常规
    AkideLiu
    AkideLiu
    Dec 24, 2020 via iPhone
    72
    Oracle : 别难为 MySQL,有啥事冲我来
    laicanwen
    laicanwen
    Dec 24, 2020 via Android
    73
    我在前公司做 ERP 的时候没有一个存储过程少于 300 行的,而且字段名还是无意义编号,看一个字段查一遍手册。那会要是每个同事写的存储过程能有这样我就谢天谢地了。
    Lancerer
    Lancerer
    Dec 24, 2020
    74
    这玩意在 mybatis 里的 xml 看才是难受,先把他转成 sql 再说吧。。。
    ming7435
    ming7435
    Dec 24, 2020
    75
    我直接就好家伙,这哥们一辈子不会被辞退了,除非公司倒闭~
    weizhen199
    weizhen199
    Dec 24, 2020
    76
    一万行的 SP 看过,一句写了三百行的还蛮勇的
    polymerdg
    polymerdg
    Dec 24, 2020
    77
    才几百行,这里 1000 行以上的 多得很
    cnzjl
    cnzjl
    Dec 24, 2020
    78
    挺正常的
    murmur
    murmur
    Dec 24, 2020
    79
    直观感觉还行,格式良好,命名工整,就是看不懂高端 SQL
    另外,我真听过 SQL 写业务的(存储过程),美其名曰直接热更不需要重启,我竟然无话可说
    chengz
    chengz
    Dec 24, 2020
    80
    其实还好,缩进正常的还是很好看的
    只是新的互联网应用这种查询可能比较少见
    x66
    x66
    Dec 24, 2020
    81
    ERP 报表开发者的日常,这也就一般难度的 sql 吧
    MoccaCafe
    MoccaCafe
    Dec 24, 2020 via iPhone
    82
    这个还行吧,起码没有各种存储过程和未知名的临时表,基本看得懂。 如果联表过于复杂,得考虑是否设计欠佳,业务跟不上
    ZinWUT
    ZinWUT
    Dec 24, 2020
    83
    ERP 业务的日常,hhh
    tianhei826
    tianhei826
    Dec 24, 2020
    84
    写个 MERGE 那么难吗。。。。。
    c6h6benzene
    c6h6benzene
    Dec 24, 2020 via iPhone
    85
    300 行,这才刚开始写吧。除了 Select *之外好像也没啥好吐槽的了。
    jorneyr
    jorneyr
    Dec 24, 2020
    86
    这是人才,得好好保护
    Nicoco
    Nicoco
    Dec 24, 2020
    87
    感觉,再搞搞,是不是会崩溃
    Achiii
    Achiii
    Dec 24, 2020
    88
    数仓抽数的 sql 都很长
    junwind
    junwind
    Dec 24, 2020
    89
    虽然不建议这么做,但是写得出来这样的 sql,也是牛啊
    kimqcn
    kimqcn
    Dec 24, 2020
    90
    交给最新的量子霸权计算机处理看看
    neptuno
    neptuno
    Dec 24, 2020
    91
    楼上各位都不审题吗,,这是用户行为可以随意触发的,,,肯定不能这样写呀
    ojbkojbk
    ojbkojbk
    Dec 24, 2020
    92
    正常操作 之前写报表比这还骚
    solaya
    solaya
    Dec 24, 2020
    93
    这个 SQL 写的不错
    onice
    onice
    Dec 24, 2020
    94
    看能算你输,所以我没看懂,你赢了。这一坨写的真好,我都看不懂。
    jitongxi
    jitongxi
    Dec 24, 2020
    95
    五千行的存储过程都见过, 这算啥
    Frank9621
    Frank9621
    Dec 24, 2020
    96
    兄弟,你这样直接把 sql 贴出来不好吧
    funbox
    funbox
    OP
    Dec 24, 2020
    97
    @Frank9621 有些字段 表前缀已经替换~~
    yanue
    yanue
    Dec 24, 2020
    98
    这不是应该算一行吗
    clf
    clf
    Dec 24, 2020
    99
    估计是可视化的 SQL 查询构造器构造的。
    runninghipp
    runninghipp
    Dec 24, 2020
    100
    这个是报表数据把
    1  2  
    ❮ ❯
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1753 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 183ms · UTC 16:19 · PVG 00:19 · LAX 08:19 · JFK 11:19
    ♥ Do have faith in what you're doing.