0

In a simple scenario, where I have many measurements table that describe the measurements that comes from a specific protocol and every measurement is linked to the respective parent device, I was looking for an optimization of my database, putting all the measurements into a table and reference from this table the respective device.

Now, keeping in mind that this table is heavily used in read/write and especially in filtering operations, I was looking for a smarter solution...I've found the Django Generic Relations and Reverse Relations, but this approach it doesn't convince me at all because, as now, with a simple select with a where the problem is solved, but using a generic relation, the ORM will generate a lot of inner joins (if I'm not wrong) and this is an aspect that I would want to avoid.

So, anyone with smarter idea?

My goal is to remove tons of if in my code that at this time are used to decide what table it will be queried based on the protocol type, as shown below:

#MODBUS
if smartdevice_device_parent == 'smart_device_modbus':
    ...Select from the right table and serialize the data...

#RS485
elif smartdevice_device_parent == 'smart_device_rs485':
    ...Select from the right table and serialize the data...

#OPC
elif smartdevice_device_parent == 'smart_device_opc':
    ...Select from the right table and serialize the data...

#MQTT
elif smartdevice_device_parent == 'smart_device_mqtt':
    ...Select from the right table and serialize the data...
else:
    return JsonResponse({'success': True, 'message': (_(ERROR)),
                         'data': ''})
  • sounds like a single table with a column smartdevice_device_parent. Then it's as simple as e.g. select .. from ... where smartdevice_device_parent = 'smart_device_mqtt' – a_horse_with_no_name Jul 18 at 6:12
  • Yeah it will be a great idea, sir – Golfista Jul 18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.