MySQL优化之IP地址的存储

背景:spring boot +Mybatis + MySql设计一个管理系统。

数据库表设计:

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`device_name`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`device_address`  varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`device_id`  bigint(64) NULL DEFAULT NULL ,
`device_ip`  int(32) NULL DEFAULT NULL ,
`device_longitude`  double(100,0) NULL DEFAULT NULL ,
`device_latitude`  double(100,0) NULL DEFAULT NULL ,
`data_time`  char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0000-00-00 00:00:00' ,
PRIMARY KEY (`id`),
UNIQUE INDEX `device_id_index` (`device_id`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=13
ROW_FORMAT=DYNAMIC
;

device_ip :表示设备的ip地址,使用int类保存。

使用SQL语句转换示例:

mapper.xml中增删改查怎么写?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.potevio.additonal.deviceManagement.mapper.EdgeDeviceMapper">
    <resultMap id="edgeDevice" type="com.potevio.additonal.deviceManagement.dao.EdgeDevice">
        <id property="id" column="id" />
        <result property="device_name" column="device_name"/>
        <result property="device_address" column="device_address"/>
        <result property="device_id" column="device_id"/>
        <result property="device_ip" column="device_ip"/>
        <result property="device_longitude" column="device_longitude"/>
        <result property="device_latitude" column="device_latitude"/>
        <result property="data_time" column="data_time"/>
    </resultMap>

    <insert id="addEdgeDevice" parameterType="com.potevio.additonal.deviceManagement.dao.EdgeDevice">
        INSERT INTO edge_device(id,device_name,device_address,device_id,device_ip,device_longitude,device_latitude,data_time)
        VALUES(#{id},#{device_name},#{device_address},#{device_id},INET_ATON(#{device_ip}),#{device_longitude},#{device_latitude},#{data_time})
    </insert>

    <delete id="deleteDevice" parameterType="long">
        DELETE FROM edge_device WHERE id = #{id}
    </delete>

    <select id="queryEdgeDeviceList" resultType="com.potevio.additonal.deviceManagement.dao.EdgeDevice">
        SELECT id,device_name,device_address,device_id, INET_NTOA(device_ip) AS device_ip ,device_longitude,device_latitude,data_time  FROM edge_device;
    </select>

    <select id="queryByDeviceId" parameterType="long" resultType="com.potevio.additonal.deviceManagement.dao.EdgeDevice">
        SELECT id,device_name,device_address,device_id, INET_NTOA(device_ip) AS device_ip ,device_longitude,device_latitude,data_time FROM edge_device WHERE device_id = #{deviceId}
    </select>

    <update id="updateDevice" parameterType="com.potevio.additonal.deviceManagement.dao.EdgeDevice">
        update edge_device set
        <if test="edgeDevice.id != null">id = #{edgeDevice.id} </if>
        <if test="edgeDevice.device_name != null">, device_name = #{edgeDevice.device_name} </if>
        <if test="edgeDevice.device_id != null">, device_id = #{edgeDevice.device_id} </if>
        <if test="edgeDevice.device_ip != null">, device_ip = INET_ATON(#{(edgeDevice.device_ip}) </if>
        <if test="edgeDevice.device_address != null">, device_address = #{edgeDevice.device_address} </if>
        <if test="edgeDevice.device_longitude != null">, device_longitude = #{edgeDevice.device_longitude} </if>
        <if test="edgeDevice.device_latitude != null">, device_latitude = #{edgeDevice.device_latitude} </if>
        <if test="edgeDevice.data_time != null">, data_time = #{edgeDevice.data_time} </if>
        where device_id = #{edgeDevice.device_id}
    </update>

    <resultMap id="deviceMessage" type="com.potevio.additonal.deviceManagement.dao.DeviceMessage">
        <result property="device_name" column="device_name"/>
        <result property="device_id" column="device_id"/>
    </resultMap>

    <select id="deviceList" resultMap="deviceMessage">
        SELECT device_name, device_id FROM edge_device;
    </select>

</mapper>

 重点:INET_ATON(),INET_NTOA()两个函数的使用,前者可以将char类型转成Int类型,后者可以将Int类型转成char类型。

为什么要多此一举?

很多人会说我在数据库中直接保存成char(32)或者varchar类型,就不用这么折腾了。的确可以,如果数据库不做优化,不追求性能是完全可以的。但是在数据库中使用int保存好处是显而易见的,查找更快,更节省空间。

发布了85 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41670928/article/details/103779072
今日推荐